]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_mir_build/src/thir/cx/mod.rs
f7351a4caa9545250d59dcbf5c21a146724fcc27
[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::pattern::pat_from_hir;
6 use crate::thir::util::UserAnnotatedTyHelpers;
7
8 use rustc_data_structures::steal::Steal;
9 use rustc_errors::ErrorGuaranteed;
10 use rustc_hir as hir;
11 use rustc_hir::def_id::{DefId, LocalDefId};
12 use rustc_hir::HirId;
13 use rustc_hir::Node;
14 use rustc_middle::middle::region;
15 use rustc_middle::thir::*;
16 use rustc_middle::ty::{self, RvalueScopes, TyCtxt};
17 use rustc_span::Span;
18
19 pub(crate) fn thir_body<'tcx>(
20 tcx: TyCtxt<'tcx>,
21 owner_def: ty::WithOptConstParam<LocalDefId>,
22 ) -> Result<(&'tcx Steal<Thir<'tcx>>, ExprId), ErrorGuaranteed> {
23 let hir = tcx.hir();
24 let body = hir.body(hir.body_owned_by(owner_def.did));
25 let mut cx = Cx::new(tcx, owner_def);
26 if let Some(reported) = cx.typeck_results.tainted_by_errors {
27 return Err(reported);
28 }
29 let expr = cx.mirror_expr(&body.value);
30 Ok((tcx.alloc_steal_thir(cx.thir), expr))
31 }
32
33 pub(crate) fn thir_tree<'tcx>(
34 tcx: TyCtxt<'tcx>,
35 owner_def: ty::WithOptConstParam<LocalDefId>,
36 ) -> String {
37 match thir_body(tcx, owner_def) {
38 Ok((thir, _)) => format!("{:#?}", thir.steal()),
39 Err(_) => "error".into(),
40 }
41 }
42
43 struct Cx<'tcx> {
44 tcx: TyCtxt<'tcx>,
45 thir: Thir<'tcx>,
46
47 pub(crate) param_env: ty::ParamEnv<'tcx>,
48
49 pub(crate) region_scope_tree: &'tcx region::ScopeTree,
50 pub(crate) typeck_results: &'tcx ty::TypeckResults<'tcx>,
51 pub(crate) rvalue_scopes: &'tcx RvalueScopes,
52
53 /// When applying adjustments to the expression
54 /// with the given `HirId`, use the given `Span`,
55 /// instead of the usual span. This is used to
56 /// assign the span of an overall method call
57 /// (e.g. `my_val.foo()`) to the adjustment expressions
58 /// for the receiver.
59 adjustment_span: Option<(HirId, Span)>,
60
61 /// The `DefId` of the owner of this body.
62 body_owner: DefId,
63 }
64
65 impl<'tcx> Cx<'tcx> {
66 fn new(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalDefId>) -> Cx<'tcx> {
67 let typeck_results = tcx.typeck_opt_const_arg(def);
68 Cx {
69 tcx,
70 thir: Thir::new(),
71 param_env: tcx.param_env(def.did),
72 region_scope_tree: tcx.region_scope_tree(def.did),
73 typeck_results,
74 rvalue_scopes: &typeck_results.rvalue_scopes,
75 body_owner: def.did.to_def_id(),
76 adjustment_span: None,
77 }
78 }
79
80 #[tracing::instrument(level = "debug", skip(self))]
81 pub(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) => 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;