2023-03-03 17:08:49 -08:00
|
|
|
//! Module that implements the bridge between Stable MIR and internal compiler MIR.
|
|
|
|
|
//!
|
|
|
|
|
//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
|
|
|
|
|
//! until stable MIR is complete.
|
|
|
|
|
|
2023-06-13 15:08:14 -07:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
use std::string::ToString;
|
|
|
|
|
|
2023-04-24 00:47:01 +00:00
|
|
|
use crate::{
|
|
|
|
|
rustc_smir::Tables,
|
|
|
|
|
stable_mir::{self, with},
|
|
|
|
|
};
|
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2023-03-07 12:47:25 -08:00
|
|
|
pub use rustc_span::def_id::{CrateNum, DefId};
|
2023-03-03 17:08:49 -08:00
|
|
|
|
2023-04-24 00:47:01 +00:00
|
|
|
fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
|
|
|
|
|
let mut ret = None;
|
|
|
|
|
with(|tables| tables.rustc_tables(&mut |t| ret = Some(f(t))));
|
|
|
|
|
ret.unwrap()
|
|
|
|
|
}
|
2023-03-16 14:53:57 +00:00
|
|
|
|
2023-03-07 12:47:25 -08:00
|
|
|
pub fn item_def_id(item: &stable_mir::CrateItem) -> DefId {
|
2023-04-24 00:47:01 +00:00
|
|
|
with_tables(|t| t.item_def_id(item))
|
2023-03-16 14:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn crate_item(did: DefId) -> stable_mir::CrateItem {
|
2023-04-24 00:47:01 +00:00
|
|
|
with_tables(|t| t.crate_item(did))
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 16:24:33 -03:00
|
|
|
pub fn adt_def(did: DefId) -> stable_mir::ty::AdtDef {
|
|
|
|
|
with_tables(|t| t.adt_def(did))
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 11:18:40 -03:00
|
|
|
pub fn foreign_def(did: DefId) -> stable_mir::ty::ForeignDef {
|
|
|
|
|
with_tables(|t| t.foreign_def(did))
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-24 00:47:01 +00:00
|
|
|
impl<'tcx> Tables<'tcx> {
|
|
|
|
|
pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
|
|
|
|
|
self.def_ids[item.0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
|
2023-07-12 16:24:33 -03:00
|
|
|
stable_mir::CrateItem(self.create_def_id(did))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn adt_def(&mut self, did: DefId) -> stable_mir::ty::AdtDef {
|
|
|
|
|
stable_mir::ty::AdtDef(self.create_def_id(did))
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 11:18:40 -03:00
|
|
|
pub fn foreign_def(&mut self, did: DefId) -> stable_mir::ty::ForeignDef {
|
|
|
|
|
stable_mir::ty::ForeignDef(self.create_def_id(did))
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 16:24:33 -03:00
|
|
|
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
|
2023-04-24 00:47:01 +00:00
|
|
|
// FIXME: this becomes inefficient when we have too many ids
|
|
|
|
|
for (i, &d) in self.def_ids.iter().enumerate() {
|
|
|
|
|
if d == did {
|
2023-07-12 16:24:33 -03:00
|
|
|
return i;
|
2023-04-24 00:47:01 +00:00
|
|
|
}
|
2023-03-16 14:53:57 +00:00
|
|
|
}
|
2023-04-24 00:47:01 +00:00
|
|
|
let id = self.def_ids.len();
|
|
|
|
|
self.def_ids.push(did);
|
2023-07-12 16:24:33 -03:00
|
|
|
id
|
2023-03-16 14:53:57 +00:00
|
|
|
}
|
2023-03-03 17:08:49 -08:00
|
|
|
}
|
2023-03-07 12:47:25 -08:00
|
|
|
|
|
|
|
|
pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
|
|
|
|
|
item.id.into()
|
|
|
|
|
}
|
2023-04-24 00:47:01 +00:00
|
|
|
|
|
|
|
|
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
|
2023-04-24 01:04:44 +00:00
|
|
|
crate::stable_mir::run(Tables { tcx, def_ids: vec![], types: vec![] }, f);
|
2023-04-24 00:47:01 +00:00
|
|
|
}
|
2023-06-13 15:08:14 -07:00
|
|
|
|
|
|
|
|
/// A type that provides internal information but that can still be used for debug purpose.
|
|
|
|
|
pub type Opaque = impl Debug + ToString + Clone;
|
|
|
|
|
|
|
|
|
|
pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
|
|
|
|
|
format!("{value:?}")
|
|
|
|
|
}
|