2023-03-03 17:08:49 -08:00
|
|
|
//! Module that implements the public interface to the Stable MIR.
|
|
|
|
//!
|
2023-08-21 09:55:27 +02:00
|
|
|
//! This module shall contain all type definitions and APIs that we expect third-party tools to invoke to
|
2023-03-03 17:08:49 -08:00
|
|
|
//! interact with the compiler.
|
|
|
|
//!
|
|
|
|
//! The goal is to eventually move this module to its own crate which shall be published on
|
|
|
|
//! [crates.io](https://crates.io).
|
|
|
|
//!
|
|
|
|
//! ## Note:
|
|
|
|
//!
|
|
|
|
//! There shouldn't be any direct references to internal compiler constructs in this module.
|
|
|
|
//! If you need an internal construct, consider using `rustc_internal` or `rustc_smir`.
|
|
|
|
|
2023-04-24 00:47:01 +00:00
|
|
|
use std::cell::Cell;
|
|
|
|
|
|
|
|
use crate::rustc_smir::Tables;
|
|
|
|
|
2023-08-22 01:09:10 -04:00
|
|
|
use self::ty::{
|
|
|
|
GenericDef, Generics, ImplDef, ImplTrait, PredicateKind, Span, TraitDecl, TraitDef, Ty, TyKind,
|
|
|
|
};
|
2023-04-24 00:47:01 +00:00
|
|
|
|
2023-03-16 16:06:12 +00:00
|
|
|
pub mod mir;
|
2023-04-24 00:47:01 +00:00
|
|
|
pub mod ty;
|
2023-03-16 16:06:12 +00:00
|
|
|
|
2023-03-03 17:08:49 -08:00
|
|
|
/// Use String for now but we should replace it.
|
|
|
|
pub type Symbol = String;
|
|
|
|
|
|
|
|
/// The number that identifies a crate.
|
|
|
|
pub type CrateNum = usize;
|
|
|
|
|
|
|
|
/// A unique identification number for each item accessible for the current compilation unit.
|
|
|
|
pub type DefId = usize;
|
|
|
|
|
|
|
|
/// A list of crate items.
|
|
|
|
pub type CrateItems = Vec<CrateItem>;
|
|
|
|
|
2023-08-07 19:02:56 -03:00
|
|
|
/// A list of trait decls.
|
2023-08-04 17:44:41 -03:00
|
|
|
pub type TraitDecls = Vec<TraitDef>;
|
|
|
|
|
2023-08-07 19:03:05 -03:00
|
|
|
/// A list of impl trait decls.
|
|
|
|
pub type ImplTraitDecls = Vec<ImplDef>;
|
|
|
|
|
2023-08-22 01:09:10 -04:00
|
|
|
/// A list of predicates.
|
|
|
|
pub struct GenericPredicates {
|
|
|
|
pub parent: Option<TraitDef>,
|
|
|
|
pub predicates: Vec<(PredicateKind, Span)>,
|
|
|
|
}
|
|
|
|
|
2023-03-03 17:08:49 -08:00
|
|
|
/// Holds information about a crate.
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct Crate {
|
|
|
|
pub(crate) id: CrateNum,
|
|
|
|
pub name: Symbol,
|
|
|
|
pub is_local: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Holds information about an item in the crate.
|
|
|
|
/// For now, it only stores the item DefId. Use functions inside `rustc_internal` module to
|
|
|
|
/// use this item.
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2023-03-16 14:53:57 +00:00
|
|
|
pub struct CrateItem(pub(crate) DefId);
|
2023-03-03 17:08:49 -08:00
|
|
|
|
2023-03-16 16:06:12 +00:00
|
|
|
impl CrateItem {
|
|
|
|
pub fn body(&self) -> mir::Body {
|
2023-04-24 00:47:01 +00:00
|
|
|
with(|cx| cx.mir_body(self))
|
2023-03-16 16:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 07:28:56 +00:00
|
|
|
/// Return the function where execution starts if the current
|
|
|
|
/// crate defines that. This is usually `main`, but could be
|
|
|
|
/// `start` if the crate is a no-std crate.
|
|
|
|
pub fn entry_fn() -> Option<CrateItem> {
|
2023-04-24 00:47:01 +00:00
|
|
|
with(|cx| cx.entry_fn())
|
2023-04-14 07:28:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 17:08:49 -08:00
|
|
|
/// Access to the local crate.
|
|
|
|
pub fn local_crate() -> Crate {
|
2023-04-24 00:47:01 +00:00
|
|
|
with(|cx| cx.local_crate())
|
2023-03-03 17:08:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to find a crate with the given name.
|
|
|
|
pub fn find_crate(name: &str) -> Option<Crate> {
|
2023-04-24 00:47:01 +00:00
|
|
|
with(|cx| cx.find_crate(name))
|
2023-03-03 17:08:49 -08:00
|
|
|
}
|
2023-03-07 12:47:25 -08:00
|
|
|
|
|
|
|
/// Try to find a crate with the given name.
|
|
|
|
pub fn external_crates() -> Vec<Crate> {
|
2023-04-24 00:47:01 +00:00
|
|
|
with(|cx| cx.external_crates())
|
2023-03-07 12:47:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve all items in the local crate that have a MIR associated with them.
|
|
|
|
pub fn all_local_items() -> CrateItems {
|
2023-04-24 00:47:01 +00:00
|
|
|
with(|cx| cx.all_local_items())
|
|
|
|
}
|
|
|
|
|
2023-08-15 13:37:47 -03:00
|
|
|
pub fn all_trait_decls() -> TraitDecls {
|
|
|
|
with(|cx| cx.all_trait_decls())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn trait_decl(trait_def: &TraitDef) -> TraitDecl {
|
|
|
|
with(|cx| cx.trait_decl(trait_def))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn all_trait_impls() -> ImplTraitDecls {
|
|
|
|
with(|cx| cx.all_trait_impls())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn trait_impl(trait_impl: &ImplDef) -> ImplTrait {
|
|
|
|
with(|cx| cx.trait_impl(trait_impl))
|
|
|
|
}
|
|
|
|
|
2023-04-24 00:47:01 +00:00
|
|
|
pub trait Context {
|
|
|
|
fn entry_fn(&mut self) -> Option<CrateItem>;
|
|
|
|
/// Retrieve all items of the local crate that have a MIR associated with them.
|
|
|
|
fn all_local_items(&mut self) -> CrateItems;
|
|
|
|
fn mir_body(&mut self, item: &CrateItem) -> mir::Body;
|
2023-08-04 17:44:41 -03:00
|
|
|
fn all_trait_decls(&mut self) -> TraitDecls;
|
2023-08-04 17:23:26 -03:00
|
|
|
fn trait_decl(&mut self, trait_def: &TraitDef) -> TraitDecl;
|
2023-08-07 19:03:05 -03:00
|
|
|
fn all_trait_impls(&mut self) -> ImplTraitDecls;
|
|
|
|
fn trait_impl(&mut self, trait_impl: &ImplDef) -> ImplTrait;
|
2023-08-22 13:01:37 +03:00
|
|
|
fn generics_of(&mut self, generic_def: &GenericDef) -> Generics;
|
2023-08-22 01:09:10 -04:00
|
|
|
fn predicates_of(&mut self, trait_def: &TraitDef) -> GenericPredicates;
|
2023-04-24 00:47:01 +00:00
|
|
|
/// Get information about the local crate.
|
|
|
|
fn local_crate(&self) -> Crate;
|
|
|
|
/// Retrieve a list of all external crates.
|
|
|
|
fn external_crates(&self) -> Vec<Crate>;
|
|
|
|
|
|
|
|
/// Find a crate with the given name.
|
|
|
|
fn find_crate(&self, name: &str) -> Option<Crate>;
|
|
|
|
|
|
|
|
/// Obtain the representation of a type.
|
|
|
|
fn ty_kind(&mut self, ty: Ty) -> TyKind;
|
|
|
|
|
|
|
|
/// HACK: Until we have fully stable consumers, we need an escape hatch
|
|
|
|
/// to get `DefId`s out of `CrateItem`s.
|
|
|
|
fn rustc_tables(&mut self, f: &mut dyn FnMut(&mut Tables<'_>));
|
|
|
|
}
|
|
|
|
|
2023-07-02 07:48:41 +08:00
|
|
|
// A thread local variable that stores a pointer to the tables mapping between TyCtxt
|
|
|
|
// datastructures and stable MIR datastructures
|
|
|
|
scoped_thread_local! (static TLV: Cell<*mut ()>);
|
2023-04-24 00:47:01 +00:00
|
|
|
|
|
|
|
pub fn run(mut context: impl Context, f: impl FnOnce()) {
|
2023-07-02 07:48:41 +08:00
|
|
|
assert!(!TLV.is_set());
|
2023-04-24 00:47:01 +00:00
|
|
|
fn g<'a>(mut context: &mut (dyn Context + 'a), f: impl FnOnce()) {
|
2023-07-02 07:48:41 +08:00
|
|
|
let ptr: *mut () = &mut context as *mut &mut _ as _;
|
|
|
|
TLV.set(&Cell::new(ptr), || {
|
|
|
|
f();
|
|
|
|
});
|
2023-04-24 00:47:01 +00:00
|
|
|
}
|
|
|
|
g(&mut context, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads the current context and calls a function with it.
|
|
|
|
/// Do not nest these, as that will ICE.
|
|
|
|
pub(crate) fn with<R>(f: impl FnOnce(&mut dyn Context) -> R) -> R {
|
2023-07-02 07:48:41 +08:00
|
|
|
assert!(TLV.is_set());
|
|
|
|
TLV.with(|tlv| {
|
|
|
|
let ptr = tlv.get();
|
|
|
|
assert!(!ptr.is_null());
|
|
|
|
f(unsafe { *(ptr as *mut &mut dyn Context) })
|
|
|
|
})
|
2023-03-07 12:47:25 -08:00
|
|
|
}
|