]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_mir_build/src/thir/cx/mod.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.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::arena::Arena;
6 use crate::thir::util::UserAnnotatedTyHelpers;
7 use crate::thir::*;
8
9 use rustc_ast as ast;
10 use rustc_hir as hir;
11 use rustc_hir::def_id::{DefId, LocalDefId};
12 use rustc_hir::Node;
13 use rustc_middle::middle::region;
14 use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
15 use rustc_middle::ty::{self, Ty, TyCtxt};
16
17 pub fn build_thir<'thir, 'tcx>(
18 tcx: TyCtxt<'tcx>,
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 }
25
26 struct Cx<'thir, 'tcx> {
27 tcx: TyCtxt<'tcx>,
28 arena: &'thir Arena<'thir, 'tcx>,
29
30 crate param_env: ty::ParamEnv<'tcx>,
31
32 crate region_scope_tree: &'tcx region::ScopeTree,
33 crate typeck_results: &'tcx ty::TypeckResults<'tcx>,
34
35 /// The `DefId` of the owner of this body.
36 body_owner: DefId,
37 }
38
39 impl<'thir, 'tcx> Cx<'thir, 'tcx> {
40 fn new(
41 tcx: TyCtxt<'tcx>,
42 def: ty::WithOptConstParam<LocalDefId>,
43 arena: &'thir Arena<'thir, 'tcx>,
44 ) -> Cx<'thir, 'tcx> {
45 let typeck_results = tcx.typeck_opt_const_arg(def);
46 Cx {
47 tcx,
48 arena,
49 param_env: tcx.param_env(def.did),
50 region_scope_tree: tcx.region_scope_tree(def.did),
51 typeck_results,
52 body_owner: def.did.to_def_id(),
53 }
54 }
55
56 crate fn const_eval_literal(
57 &mut self,
58 lit: &'tcx ast::LitKind,
59 ty: Ty<'tcx>,
60 sp: Span,
61 neg: bool,
62 ) -> &'tcx ty::Const<'tcx> {
63 trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
64
65 match self.tcx.at(sp).lit_to_const(LitToConstInput { lit, ty, neg }) {
66 Ok(c) => c,
67 Err(LitToConstError::UnparseableFloat) => {
68 // FIXME(#31407) this is only necessary because float parsing is buggy
69 self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)");
70 // create a dummy value and continue compiling
71 self.tcx.const_error(ty)
72 }
73 Err(LitToConstError::Reported) => {
74 // create a dummy value and continue compiling
75 self.tcx.const_error(ty)
76 }
77 Err(LitToConstError::TypeError) => bug!("const_eval_literal: had type error"),
78 }
79 }
80
81 crate fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Pat<'tcx> {
82 let p = match self.tcx.hir().get(p.hir_id) {
83 Node::Pat(p) | Node::Binding(p) => p,
84 node => bug!("pattern became {:?}", node),
85 };
86 Pat::from_hir(self.tcx, self.param_env, self.typeck_results(), p)
87 }
88 }
89
90 impl<'tcx> UserAnnotatedTyHelpers<'tcx> for Cx<'_, 'tcx> {
91 fn tcx(&self) -> TyCtxt<'tcx> {
92 self.tcx
93 }
94
95 fn typeck_results(&self) -> &ty::TypeckResults<'tcx> {
96 self.typeck_results
97 }
98 }
99
100 mod block;
101 mod expr;