]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/thir/cx/mod.rs
[WIP] Eagerly construct bodies of THIR
[rust.git] / compiler / rustc_mir_build / src / thir / cx / mod.rs
1 //! This module contains the functionality to convert from the wacky tcx data
2 //! structures into the THIR. 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::thir::util::UserAnnotatedTyHelpers;
6 use crate::thir::*;
7
8 use rustc_ast as ast;
9 use rustc_hir as hir;
10 use rustc_hir::def_id::{DefId, LocalDefId};
11 use rustc_hir::Node;
12 use rustc_index::vec::Idx;
13 use rustc_infer::infer::InferCtxt;
14 use rustc_middle::middle::region;
15 use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
16 use rustc_middle::ty::subst::Subst;
17 use rustc_middle::ty::subst::{GenericArg, InternalSubsts};
18 use rustc_middle::ty::{self, Ty, TyCtxt};
19 use rustc_span::symbol::{sym, Symbol};
20 use rustc_target::abi::VariantIdx;
21 use rustc_trait_selection::infer::InferCtxtExt;
22
23 #[derive(Clone)]
24 crate struct Cx<'a, 'tcx> {
25     tcx: TyCtxt<'tcx>,
26     infcx: &'a InferCtxt<'a, 'tcx>,
27
28     crate root_lint_level: hir::HirId,
29     crate param_env: ty::ParamEnv<'tcx>,
30
31     /// Identity `InternalSubsts` for use with const-evaluation.
32     crate identity_substs: &'tcx InternalSubsts<'tcx>,
33
34     crate region_scope_tree: &'tcx region::ScopeTree,
35     crate typeck_results: &'a ty::TypeckResults<'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     crate body_owner_kind: hir::BodyOwnerKind,
46
47     /// Whether this constant/function needs overflow checks.
48     check_overflow: bool,
49 }
50
51 impl<'a, 'tcx> Cx<'a, 'tcx> {
52     crate fn new(
53         infcx: &'a InferCtxt<'a, 'tcx>,
54         def: ty::WithOptConstParam<LocalDefId>,
55         src_id: hir::HirId,
56     ) -> Cx<'a, 'tcx> {
57         let tcx = infcx.tcx;
58         let typeck_results = tcx.typeck_opt_const_arg(def);
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 = tcx.sess.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(def.did),
84             identity_substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
85             region_scope_tree: tcx.region_scope_tree(def.did),
86             typeck_results,
87             constness,
88             body_owner: def.did.to_def_id(),
89             body_owner_kind,
90             check_overflow,
91         }
92     }
93 }
94
95 impl<'a, 'tcx> Cx<'a, 'tcx> {
96     crate fn usize_ty(&mut self) -> Ty<'tcx> {
97         self.tcx.types.usize
98     }
99
100     crate fn usize_literal(&mut self, value: u64) -> &'tcx ty::Const<'tcx> {
101         ty::Const::from_usize(self.tcx, value)
102     }
103
104     crate fn bool_ty(&mut self) -> Ty<'tcx> {
105         self.tcx.types.bool
106     }
107
108     crate fn unit_ty(&mut self) -> Ty<'tcx> {
109         self.tcx.mk_unit()
110     }
111
112     crate fn true_literal(&mut self) -> &'tcx ty::Const<'tcx> {
113         ty::Const::from_bool(self.tcx, true)
114     }
115
116     crate fn false_literal(&mut self) -> &'tcx ty::Const<'tcx> {
117         ty::Const::from_bool(self.tcx, false)
118     }
119
120     crate fn const_eval_literal(
121         &mut self,
122         lit: &'tcx ast::LitKind,
123         ty: Ty<'tcx>,
124         sp: Span,
125         neg: bool,
126     ) -> &'tcx ty::Const<'tcx> {
127         trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
128
129         match self.tcx.at(sp).lit_to_const(LitToConstInput { lit, ty, neg }) {
130             Ok(c) => c,
131             Err(LitToConstError::UnparseableFloat) => {
132                 // FIXME(#31407) this is only necessary because float parsing is buggy
133                 self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)");
134                 // create a dummy value and continue compiling
135                 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
136             }
137             Err(LitToConstError::Reported) => {
138                 // create a dummy value and continue compiling
139                 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
140             }
141             Err(LitToConstError::TypeError) => bug!("const_eval_literal: had type error"),
142         }
143     }
144
145     crate fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Pat<'tcx> {
146         let p = match self.tcx.hir().get(p.hir_id) {
147             Node::Pat(p) | Node::Binding(p) => p,
148             node => bug!("pattern became {:?}", node),
149         };
150         Pat::from_hir(self.tcx, self.param_env, self.typeck_results(), p)
151     }
152
153     crate fn trait_method(
154         &mut self,
155         trait_def_id: DefId,
156         method_name: Symbol,
157         self_ty: Ty<'tcx>,
158         params: &[GenericArg<'tcx>],
159     ) -> &'tcx ty::Const<'tcx> {
160         let substs = self.tcx.mk_substs_trait(self_ty, params);
161
162         // The unhygienic comparison here is acceptable because this is only
163         // used on known traits.
164         let item = self
165             .tcx
166             .associated_items(trait_def_id)
167             .filter_by_name_unhygienic(method_name)
168             .find(|item| item.kind == ty::AssocKind::Fn)
169             .expect("trait method not found");
170
171         let method_ty = self.tcx.type_of(item.def_id);
172         let method_ty = method_ty.subst(self.tcx, substs);
173         ty::Const::zero_sized(self.tcx, method_ty)
174     }
175
176     crate fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: VariantIdx) -> Vec<Field> {
177         (0..adt_def.variants[variant_index].fields.len()).map(Field::new).collect()
178     }
179
180     crate fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
181         ty.needs_drop(self.tcx, self.param_env)
182     }
183
184     crate fn infcx(&self) -> &'a InferCtxt<'a, 'tcx> {
185         self.infcx
186     }
187
188     crate fn tcx(&self) -> TyCtxt<'tcx> {
189         self.tcx
190     }
191
192     crate fn typeck_results(&self) -> &'a ty::TypeckResults<'tcx> {
193         self.typeck_results
194     }
195
196     crate fn check_overflow(&self) -> bool {
197         self.check_overflow
198     }
199
200     crate fn type_is_copy_modulo_regions(&self, ty: Ty<'tcx>, span: Span) -> bool {
201         self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span)
202     }
203 }
204
205 impl<'tcx> UserAnnotatedTyHelpers<'tcx> for Cx<'_, 'tcx> {
206     fn tcx(&self) -> TyCtxt<'tcx> {
207         self.tcx()
208     }
209
210     fn typeck_results(&self) -> &ty::TypeckResults<'tcx> {
211         self.typeck_results()
212     }
213 }
214
215 mod block;
216 mod expr;