rustc_smir/rustc_smir/
mod.rs1use std::ops::RangeInclusive;
11
12use rustc_hir::def::DefKind;
13use rustc_middle::mir;
14use rustc_middle::mir::interpret::AllocId;
15use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
16use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
17use stable_mir::abi::Layout;
18use stable_mir::mir::mono::{InstanceDef, StaticDef};
19use stable_mir::ty::{FnDef, MirConstId, Span, TyConstId};
20use stable_mir::{CtorKind, ItemKind};
21use tracing::debug;
22
23use crate::rustc_internal::IndexMap;
24use crate::stable_mir;
25
26mod alloc;
27mod builder;
28pub mod context;
29mod convert;
30
31pub struct Tables<'tcx> {
32 pub(crate) tcx: TyCtxt<'tcx>,
33 pub(crate) def_ids: IndexMap<DefId, stable_mir::DefId>,
34 pub(crate) alloc_ids: IndexMap<AllocId, stable_mir::mir::alloc::AllocId>,
35 pub(crate) spans: IndexMap<rustc_span::Span, Span>,
36 pub(crate) types: IndexMap<Ty<'tcx>, stable_mir::ty::Ty>,
37 pub(crate) instances: IndexMap<ty::Instance<'tcx>, InstanceDef>,
38 pub(crate) ty_consts: IndexMap<ty::Const<'tcx>, TyConstId>,
39 pub(crate) mir_consts: IndexMap<mir::Const<'tcx>, MirConstId>,
40 pub(crate) layouts: IndexMap<rustc_abi::Layout<'tcx>, Layout>,
41}
42
43impl<'tcx> Tables<'tcx> {
44 pub(crate) fn intern_ty(&mut self, ty: Ty<'tcx>) -> stable_mir::ty::Ty {
45 self.types.create_or_fetch(ty)
46 }
47
48 pub(crate) fn intern_ty_const(&mut self, ct: ty::Const<'tcx>) -> TyConstId {
49 self.ty_consts.create_or_fetch(ct)
50 }
51
52 pub(crate) fn intern_mir_const(&mut self, constant: mir::Const<'tcx>) -> MirConstId {
53 self.mir_consts.create_or_fetch(constant)
54 }
55
56 pub(crate) fn instance_has_body(&self, instance: Instance<'tcx>) -> bool {
61 let def_id = instance.def_id();
62 self.item_has_body(def_id)
63 || !matches!(
64 instance.def,
65 ty::InstanceKind::Virtual(..)
66 | ty::InstanceKind::Intrinsic(..)
67 | ty::InstanceKind::Item(..)
68 )
69 }
70
71 pub(crate) fn item_has_body(&self, def_id: DefId) -> bool {
76 let must_override = if let Some(intrinsic) = self.tcx.intrinsic(def_id) {
77 intrinsic.must_be_overridden
78 } else {
79 false
80 };
81 !must_override && self.tcx.is_mir_available(def_id)
82 }
83
84 fn to_fn_def(&mut self, def_id: DefId) -> Option<FnDef> {
85 if matches!(self.tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
86 Some(self.fn_def(def_id))
87 } else {
88 None
89 }
90 }
91
92 fn to_static(&mut self, def_id: DefId) -> Option<StaticDef> {
93 matches!(self.tcx.def_kind(def_id), DefKind::Static { .. }).then(|| self.static_def(def_id))
94 }
95}
96
97pub(crate) fn filter_def_ids<F, T>(tcx: TyCtxt<'_>, krate: CrateNum, mut func: F) -> Vec<T>
99where
100 F: FnMut(DefId) -> Option<T>,
101{
102 if krate == LOCAL_CRATE {
103 tcx.iter_local_def_id().filter_map(|did| func(did.to_def_id())).collect()
104 } else {
105 let num_definitions = tcx.num_extern_def_ids(krate);
106 (0..num_definitions)
107 .filter_map(move |i| {
108 let def_id = DefId { krate, index: rustc_span::def_id::DefIndex::from_usize(i) };
109 func(def_id)
110 })
111 .collect()
112 }
113}
114
115pub(crate) fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
117 let crate_name = tcx.crate_name(crate_num).to_string();
118 let is_local = crate_num == LOCAL_CRATE;
119 debug!(?crate_name, ?crate_num, "smir_crate");
120 stable_mir::Crate { id: crate_num.into(), name: crate_name, is_local }
121}
122
123pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
124 match kind {
125 DefKind::Mod
126 | DefKind::Struct
127 | DefKind::Union
128 | DefKind::Enum
129 | DefKind::Variant
130 | DefKind::Trait
131 | DefKind::TyAlias
132 | DefKind::ForeignTy
133 | DefKind::TraitAlias
134 | DefKind::AssocTy
135 | DefKind::TyParam
136 | DefKind::ConstParam
137 | DefKind::Macro(_)
138 | DefKind::ExternCrate
139 | DefKind::Use
140 | DefKind::ForeignMod
141 | DefKind::OpaqueTy
142 | DefKind::Field
143 | DefKind::LifetimeParam
144 | DefKind::Impl { .. }
145 | DefKind::GlobalAsm => {
146 unreachable!("Not a valid item kind: {kind:?}");
147 }
148 DefKind::Closure | DefKind::AssocFn | DefKind::Fn | DefKind::SyntheticCoroutineBody => {
149 ItemKind::Fn
150 }
151 DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => {
152 ItemKind::Const
153 }
154 DefKind::Static { .. } => ItemKind::Static,
155 DefKind::Ctor(_, rustc_hir::def::CtorKind::Const) => ItemKind::Ctor(CtorKind::Const),
156 DefKind::Ctor(_, rustc_hir::def::CtorKind::Fn) => ItemKind::Ctor(CtorKind::Fn),
157 }
158}
159
160pub trait Stable<'cx> {
162 type T;
164 fn stable(&self, tables: &mut Tables<'_>) -> Self::T;
166}
167
168impl<'tcx, T> Stable<'tcx> for &T
169where
170 T: Stable<'tcx>,
171{
172 type T = T::T;
173
174 fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
175 (*self).stable(tables)
176 }
177}
178
179impl<'tcx, T> Stable<'tcx> for Option<T>
180where
181 T: Stable<'tcx>,
182{
183 type T = Option<T::T>;
184
185 fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
186 self.as_ref().map(|value| value.stable(tables))
187 }
188}
189
190impl<'tcx, T, E> Stable<'tcx> for Result<T, E>
191where
192 T: Stable<'tcx>,
193 E: Stable<'tcx>,
194{
195 type T = Result<T::T, E::T>;
196
197 fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
198 match self {
199 Ok(val) => Ok(val.stable(tables)),
200 Err(error) => Err(error.stable(tables)),
201 }
202 }
203}
204
205impl<'tcx, T> Stable<'tcx> for &[T]
206where
207 T: Stable<'tcx>,
208{
209 type T = Vec<T::T>;
210 fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
211 self.iter().map(|e| e.stable(tables)).collect()
212 }
213}
214
215impl<'tcx, T, U> Stable<'tcx> for (T, U)
216where
217 T: Stable<'tcx>,
218 U: Stable<'tcx>,
219{
220 type T = (T::T, U::T);
221 fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
222 (self.0.stable(tables), self.1.stable(tables))
223 }
224}
225
226impl<'tcx, T> Stable<'tcx> for RangeInclusive<T>
227where
228 T: Stable<'tcx>,
229{
230 type T = RangeInclusive<T::T>;
231 fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
232 RangeInclusive::new(self.start().stable(tables), self.end().stable(tables))
233 }
234}