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