]>
Commit | Line | Data |
---|---|---|
3dfed10e XL |
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, | |
9fa01778 | 3 | //! etc., and instead goes through the `Cx` for most of its work. |
b039eaaf | 4 | |
6a06907d | 5 | use crate::thir::arena::Arena; |
3dfed10e XL |
6 | use crate::thir::util::UserAnnotatedTyHelpers; |
7 | use crate::thir::*; | |
b039eaaf | 8 | |
3dfed10e | 9 | use rustc_ast as ast; |
dfeec247 | 10 | use rustc_hir as hir; |
3dfed10e | 11 | use rustc_hir::def_id::{DefId, LocalDefId}; |
dfeec247 | 12 | use rustc_hir::Node; |
ba9703b0 XL |
13 | use rustc_middle::middle::region; |
14 | use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput}; | |
ba9703b0 | 15 | use rustc_middle::ty::{self, Ty, TyCtxt}; |
e9174d1e | 16 | |
6a06907d | 17 | pub fn build_thir<'thir, 'tcx>( |
dc9dc135 | 18 | tcx: TyCtxt<'tcx>, |
6a06907d XL |
19 | owner_def: ty::WithOptConstParam<LocalDefId>, |
20 | arena: &'thir Arena<'thir, 'tcx>, | |
21 | expr: &'tcx hir::Expr<'tcx>, | |
22 | ) -> &'thir Expr<'thir, 'tcx> { | |
23 | Cx::new(tcx, owner_def, &arena).mirror_expr(expr) | |
24 | } | |
3b2f2976 | 25 | |
6a06907d XL |
26 | struct Cx<'thir, 'tcx> { |
27 | tcx: TyCtxt<'tcx>, | |
28 | arena: &'thir Arena<'thir, 'tcx>, | |
3b2f2976 | 29 | |
6a06907d | 30 | crate param_env: ty::ParamEnv<'tcx>, |
3b2f2976 | 31 | |
dfeec247 | 32 | crate region_scope_tree: &'tcx region::ScopeTree, |
6a06907d | 33 | crate typeck_results: &'tcx ty::TypeckResults<'tcx>, |
3157f602 | 34 | |
dc9dc135 XL |
35 | /// The `DefId` of the owner of this body. |
36 | body_owner: DefId, | |
e9174d1e SL |
37 | } |
38 | ||
6a06907d XL |
39 | impl<'thir, 'tcx> Cx<'thir, 'tcx> { |
40 | fn new( | |
41 | tcx: TyCtxt<'tcx>, | |
3dfed10e | 42 | def: ty::WithOptConstParam<LocalDefId>, |
6a06907d XL |
43 | arena: &'thir Arena<'thir, 'tcx>, |
44 | ) -> Cx<'thir, 'tcx> { | |
3dfed10e | 45 | let typeck_results = tcx.typeck_opt_const_arg(def); |
3b2f2976 XL |
46 | Cx { |
47 | tcx, | |
6a06907d | 48 | arena, |
3dfed10e | 49 | param_env: tcx.param_env(def.did), |
3dfed10e XL |
50 | region_scope_tree: tcx.region_scope_tree(def.did), |
51 | typeck_results, | |
3dfed10e | 52 | body_owner: def.did.to_def_id(), |
3b2f2976 | 53 | } |
e9174d1e | 54 | } |
b039eaaf | 55 | |
dfeec247 | 56 | crate fn const_eval_literal( |
0531ce1d XL |
57 | &mut self, |
58 | lit: &'tcx ast::LitKind, | |
59 | ty: Ty<'tcx>, | |
60 | sp: Span, | |
61 | neg: bool, | |
dc9dc135 | 62 | ) -> &'tcx ty::Const<'tcx> { |
0531ce1d | 63 | trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg); |
0531ce1d | 64 | |
dfeec247 | 65 | match self.tcx.at(sp).lit_to_const(LitToConstInput { lit, ty, neg }) { |
a1dfa0c6 XL |
66 | Ok(c) => c, |
67 | Err(LitToConstError::UnparseableFloat) => { | |
0531ce1d | 68 | // FIXME(#31407) this is only necessary because float parsing is buggy |
a1dfa0c6 XL |
69 | self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)"); |
70 | // create a dummy value and continue compiling | |
6a06907d | 71 | self.tcx.const_error(ty) |
dfeec247 | 72 | } |
a1dfa0c6 XL |
73 | Err(LitToConstError::Reported) => { |
74 | // create a dummy value and continue compiling | |
6a06907d | 75 | self.tcx.const_error(ty) |
0531ce1d | 76 | } |
dfeec247 | 77 | Err(LitToConstError::TypeError) => bug!("const_eval_literal: had type error"), |
a1dfa0c6 | 78 | } |
92a42be0 SL |
79 | } |
80 | ||
dfeec247 | 81 | crate fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Pat<'tcx> { |
e74abb32 | 82 | let p = match self.tcx.hir().get(p.hir_id) { |
b7449926 | 83 | Node::Pat(p) | Node::Binding(p) => p, |
dfeec247 | 84 | node => bug!("pattern became {:?}", node), |
ea8adc8c | 85 | }; |
3dfed10e | 86 | Pat::from_hir(self.tcx, self.param_env, self.typeck_results(), p) |
ea8adc8c | 87 | } |
e9174d1e SL |
88 | } |
89 | ||
dfeec247 | 90 | impl<'tcx> UserAnnotatedTyHelpers<'tcx> for Cx<'_, 'tcx> { |
dc9dc135 | 91 | fn tcx(&self) -> TyCtxt<'tcx> { |
6a06907d | 92 | self.tcx |
0bf4aa26 XL |
93 | } |
94 | ||
3dfed10e | 95 | fn typeck_results(&self) -> &ty::TypeckResults<'tcx> { |
6a06907d | 96 | self.typeck_results |
0bf4aa26 XL |
97 | } |
98 | } | |
99 | ||
e9174d1e SL |
100 | mod block; |
101 | mod expr; |