rustc_smir/context/
mod.rs1#![allow(rustc::usage_of_qualified_ty)]
4
5use std::marker::PhantomData;
6
7use rustc_abi::HasDataLayout;
8use rustc_middle::ty;
9use rustc_middle::ty::layout::{FnAbiOfHelpers, HasTyCtxt, HasTypingEnv, LayoutOfHelpers};
10use rustc_middle::ty::{Ty, TyCtxt};
11
12use crate::{Bridge, SmirError};
13
14mod impls;
15mod traits;
16
17pub use traits::*;
18
19pub struct SmirCtxt<'tcx, B: Bridge> {
24 pub tcx: TyCtxt<'tcx>,
25 _marker: PhantomData<B>,
26}
27
28impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
29 pub fn new(tcx: TyCtxt<'tcx>) -> Self {
30 Self { tcx, _marker: Default::default() }
31 }
32}
33
34impl<'tcx, B: Bridge> FnAbiOfHelpers<'tcx> for SmirCtxt<'tcx, B> {
36 type FnAbiOfResult = Result<&'tcx rustc_target::callconv::FnAbi<'tcx, Ty<'tcx>>, B::Error>;
37
38 #[inline]
39 fn handle_fn_abi_err(
40 &self,
41 err: ty::layout::FnAbiError<'tcx>,
42 _span: rustc_span::Span,
43 fn_abi_request: ty::layout::FnAbiRequest<'tcx>,
44 ) -> B::Error {
45 B::Error::new(format!("Failed to get ABI for `{fn_abi_request:?}`: {err:?}"))
46 }
47}
48
49impl<'tcx, B: Bridge> LayoutOfHelpers<'tcx> for SmirCtxt<'tcx, B> {
50 type LayoutOfResult = Result<ty::layout::TyAndLayout<'tcx>, B::Error>;
51
52 #[inline]
53 fn handle_layout_err(
54 &self,
55 err: ty::layout::LayoutError<'tcx>,
56 _span: rustc_span::Span,
57 ty: Ty<'tcx>,
58 ) -> B::Error {
59 B::Error::new(format!("Failed to get layout for `{ty}`: {err}"))
60 }
61}
62
63impl<'tcx, B: Bridge> HasTypingEnv<'tcx> for SmirCtxt<'tcx, B> {
64 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
65 ty::TypingEnv::fully_monomorphized()
66 }
67}
68
69impl<'tcx, B: Bridge> HasTyCtxt<'tcx> for SmirCtxt<'tcx, B> {
70 fn tcx(&self) -> TyCtxt<'tcx> {
71 self.tcx
72 }
73}
74
75impl<'tcx, B: Bridge> HasDataLayout for SmirCtxt<'tcx, B> {
76 fn data_layout(&self) -> &rustc_abi::TargetDataLayout {
77 self.tcx.data_layout()
78 }
79}