]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/hair/cx/mod.rs
move raw span to tt reader
[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;
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, InternalSubsts};
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 crate::hair::constant::{lit_to_const, LitToConstError};
22
23 #[derive(Clone)]
24 pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
25     tcx: TyCtxt<'a, 'gcx, 'tcx>,
26     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
27
28     pub root_lint_level: hir::HirId,
29     pub param_env: ty::ParamEnv<'gcx>,
30
31     /// Identity `InternalSubsts` for use with const-evaluation.
32     pub identity_substs: &'gcx InternalSubsts<'gcx>,
33
34     pub region_scope_tree: &'gcx region::ScopeTree,
35     pub tables: &'a ty::TypeckTables<'gcx>,
36
37     /// This is `Constness::Const` if we are compiling a `static`,
38     /// `const`, or the body of a `const fn`.
39     constness: hir::Constness,
40
41     /// What kind of body is being compiled.
42     pub body_owner_kind: hir::BodyOwnerKind,
43
44     /// Whether this constant/function needs overflow checks.
45     check_overflow: bool,
46
47     /// See field with the same name on `Mir`.
48     control_flow_destroyed: Vec<(Span, String)>,
49 }
50
51 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
52     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
53                src_id: hir::HirId) -> Cx<'a, 'gcx, 'tcx> {
54         let tcx = infcx.tcx;
55         let src_def_id = tcx.hir().local_def_id_from_hir_id(src_id);
56         let body_owner_kind = tcx.hir().body_owner_kind_by_hir_id(src_id);
57
58         let constness = match body_owner_kind {
59             hir::BodyOwnerKind::Const |
60             hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
61             hir::BodyOwnerKind::Closure |
62             hir::BodyOwnerKind::Fn => hir::Constness::NotConst,
63         };
64
65         let attrs = tcx.hir().attrs_by_hir_id(src_id);
66
67         // Some functions always have overflow checks enabled,
68         // however, they may not get codegen'd, depending on
69         // the settings for the crate they are codegened in.
70         let mut check_overflow = attr::contains_name(attrs, "rustc_inherit_overflow_checks");
71
72         // Respect -C overflow-checks.
73         check_overflow |= tcx.sess.overflow_checks();
74
75         // Constants always need overflow checks.
76         check_overflow |= constness == hir::Constness::Const;
77
78         Cx {
79             tcx,
80             infcx,
81             root_lint_level: src_id,
82             param_env: tcx.param_env(src_def_id),
83             identity_substs: InternalSubsts::identity_for_item(tcx.global_tcx(), src_def_id),
84             region_scope_tree: tcx.region_scope_tree(src_def_id),
85             tables: tcx.typeck_tables_of(src_def_id),
86             constness,
87             body_owner_kind,
88             check_overflow,
89             control_flow_destroyed: Vec::new(),
90         }
91     }
92
93     pub fn control_flow_destroyed(self) -> Vec<(Span, String)> {
94         self.control_flow_destroyed
95     }
96 }
97
98 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
99     /// Normalizes `ast` into the appropriate "mirror" type.
100     pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
101         ast.make_mirror(self)
102     }
103
104     pub fn usize_ty(&mut self) -> Ty<'tcx> {
105         self.tcx.types.usize
106     }
107
108     pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::Const<'tcx> {
109         self.tcx.mk_const(ty::Const::from_usize(self.tcx, value))
110     }
111
112     pub fn bool_ty(&mut self) -> Ty<'tcx> {
113         self.tcx.types.bool
114     }
115
116     pub fn unit_ty(&mut self) -> Ty<'tcx> {
117         self.tcx.mk_unit()
118     }
119
120     pub fn true_literal(&mut self) -> &'tcx ty::Const<'tcx> {
121         self.tcx.mk_const(ty::Const::from_bool(self.tcx, true))
122     }
123
124     pub fn false_literal(&mut self) -> &'tcx ty::Const<'tcx> {
125         self.tcx.mk_const(ty::Const::from_bool(self.tcx, false))
126     }
127
128     pub fn const_eval_literal(
129         &mut self,
130         lit: &'tcx ast::LitKind,
131         ty: Ty<'tcx>,
132         sp: Span,
133         neg: bool,
134     ) -> ty::Const<'tcx> {
135         trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
136
137         match lit_to_const(lit, self.tcx, ty, neg) {
138             Ok(c) => c,
139             Err(LitToConstError::UnparseableFloat) => {
140                 // FIXME(#31407) this is only necessary because float parsing is buggy
141                 self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)");
142                 // create a dummy value and continue compiling
143                 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
144             },
145             Err(LitToConstError::Reported) => {
146                 // create a dummy value and continue compiling
147                 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
148             }
149         }
150     }
151
152     pub fn pattern_from_hir(&mut self, p: &hir::Pat) -> Pattern<'tcx> {
153         let tcx = self.tcx.global_tcx();
154         let p = match tcx.hir().get_by_hir_id(p.hir_id) {
155             Node::Pat(p) | Node::Binding(p) => p,
156             node => bug!("pattern became {:?}", node)
157         };
158         Pattern::from_hir(tcx,
159                           self.param_env.and(self.identity_substs),
160                           self.tables(),
161                           p)
162     }
163
164     pub fn trait_method(&mut self,
165                         trait_def_id: DefId,
166                         method_name: &str,
167                         self_ty: Ty<'tcx>,
168                         params: &[Kind<'tcx>])
169                         -> (Ty<'tcx>, ty::Const<'tcx>) {
170         let method_name = Symbol::intern(method_name);
171         let substs = self.tcx.mk_substs_trait(self_ty, params);
172         for item in self.tcx.associated_items(trait_def_id) {
173             if item.kind == ty::AssociatedKind::Method && item.ident.name == method_name {
174                 let method_ty = self.tcx.type_of(item.def_id);
175                 let method_ty = method_ty.subst(self.tcx, substs);
176                 return (method_ty, ty::Const::zero_sized(method_ty));
177             }
178         }
179
180         bug!("found no method `{}` in `{:?}`", method_name, trait_def_id);
181     }
182
183     pub fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: VariantIdx) -> Vec<Field> {
184         (0..adt_def.variants[variant_index].fields.len())
185             .map(Field::new)
186             .collect()
187     }
188
189     pub fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
190         let (ty, param_env) = self.tcx.lift_to_global(&(ty, self.param_env)).unwrap_or_else(|| {
191             bug!("MIR: Cx::needs_drop({:?}, {:?}) got \
192                   type with inference types/regions",
193                  ty, self.param_env);
194         });
195         ty.needs_drop(self.tcx.global_tcx(), param_env)
196     }
197
198     pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
199         self.tcx
200     }
201
202     pub fn tables(&self) -> &'a ty::TypeckTables<'gcx> {
203         self.tables
204     }
205
206     pub fn check_overflow(&self) -> bool {
207         self.check_overflow
208     }
209
210     pub fn type_is_copy_modulo_regions(&self, ty: Ty<'tcx>, span: Span) -> bool {
211         self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span)
212     }
213 }
214
215 impl UserAnnotatedTyHelpers<'gcx, 'tcx> for Cx<'_, 'gcx, 'tcx> {
216     fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx> {
217         self.tcx()
218     }
219
220     fn tables(&self) -> &ty::TypeckTables<'tcx> {
221         self.tables()
222     }
223 }
224
225 mod block;
226 mod expr;
227 mod to_ref;