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