rust/compiler/rustc_mir_build/src/thir/cx/mod.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
3.1 KiB
Rust
Raw Normal View History

2020-07-06 23:49:53 +02:00
//! This module contains the functionality to convert from the wacky tcx data
2020-07-21 09:09:27 +00:00
//! structures into the THIR. The `builder` is generally ignorant of the tcx,
2019-02-08 14:53:55 +01:00
//! etc., and instead goes through the `Cx` for most of its work.
use crate::thir::pattern::pat_from_hir;
2021-04-04 18:42:17 +02:00
use crate::thir::util::UserAnnotatedTyHelpers;
2021-04-04 18:42:17 +02:00
use rustc_data_structures::steal::Steal;
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
2020-07-06 23:49:53 +02:00
use rustc_hir::def_id::{DefId, LocalDefId};
2021-09-16 15:01:22 -05:00
use rustc_hir::HirId;
use rustc_hir::Node;
2020-03-29 17:19:48 +02:00
use rustc_middle::middle::region;
use rustc_middle::thir::*;
2022-06-23 22:16:50 +02:00
use rustc_middle::ty::{self, RvalueScopes, TyCtxt};
use rustc_span::Span;
2015-08-18 17:59:21 -04:00
pub(crate) fn thir_body<'tcx>(
2019-06-14 00:48:52 +03:00
tcx: TyCtxt<'tcx>,
owner_def: ty::WithOptConstParam<LocalDefId>,
) -> Result<(&'tcx Steal<Thir<'tcx>>, ExprId), ErrorGuaranteed> {
2021-04-04 18:42:17 +02:00
let hir = tcx.hir();
let body = hir.body(hir.body_owned_by(owner_def.did));
let mut cx = Cx::new(tcx, owner_def);
if let Some(reported) = cx.typeck_results.tainted_by_errors {
return Err(reported);
2021-05-22 15:40:26 +02:00
}
2021-04-04 18:42:17 +02:00
let expr = cx.mirror_expr(&body.value);
Ok((tcx.alloc_steal_thir(cx.thir), expr))
}
pub(crate) fn thir_tree<'tcx>(
2021-07-24 17:18:15 -04:00
tcx: TyCtxt<'tcx>,
owner_def: ty::WithOptConstParam<LocalDefId>,
) -> String {
match thir_body(tcx, owner_def) {
Ok((thir, _)) => format!("{:#?}", thir.steal()),
Err(_) => "error".into(),
}
2021-07-24 17:18:15 -04:00
}
struct Cx<'tcx> {
tcx: TyCtxt<'tcx>,
thir: Thir<'tcx>,
pub(crate) param_env: ty::ParamEnv<'tcx>,
pub(crate) region_scope_tree: &'tcx region::ScopeTree,
pub(crate) typeck_results: &'tcx ty::TypeckResults<'tcx>,
pub(crate) rvalue_scopes: &'tcx RvalueScopes,
2021-09-16 15:01:22 -05:00
/// When applying adjustments to the expression
/// with the given `HirId`, use the given `Span`,
/// instead of the usual span. This is used to
/// assign the span of an overall method call
/// (e.g. `my_val.foo()`) to the adjustment expressions
/// for the receiver.
adjustment_span: Option<(HirId, Span)>,
/// The `DefId` of the owner of this body.
body_owner: DefId,
2015-08-18 17:59:21 -04:00
}
impl<'tcx> Cx<'tcx> {
fn new(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalDefId>) -> Cx<'tcx> {
2020-07-17 08:47:04 +00:00
let typeck_results = tcx.typeck_opt_const_arg(def);
Cx {
tcx,
thir: Thir::new(),
2020-07-06 23:49:53 +02:00
param_env: tcx.param_env(def.did),
region_scope_tree: tcx.region_scope_tree(def.did),
2020-07-17 08:47:04 +00:00
typeck_results,
rvalue_scopes: &typeck_results.rvalue_scopes,
2020-07-06 23:49:53 +02:00
body_owner: def.did.to_def_id(),
2021-09-16 15:01:22 -05:00
adjustment_span: None,
}
2015-08-18 17:59:21 -04:00
}
#[instrument(level = "debug", skip(self))]
pub(crate) fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Box<Pat<'tcx>> {
let p = match self.tcx.hir().get(p.hir_id) {
2022-06-28 13:15:30 -05:00
Node::Pat(p) => p,
2017-08-04 00:41:44 +03:00
node => bug!("pattern became {:?}", node),
};
pat_from_hir(self.tcx, self.param_env, self.typeck_results(), p)
2017-08-04 00:41:44 +03:00
}
2015-08-18 17:59:21 -04:00
}
impl<'tcx> UserAnnotatedTyHelpers<'tcx> for Cx<'tcx> {
2019-06-14 00:48:52 +03:00
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
2020-07-17 08:47:04 +00:00
fn typeck_results(&self) -> &ty::TypeckResults<'tcx> {
self.typeck_results
}
}
2015-08-18 17:59:21 -04:00
mod block;
mod expr;