2023-09-14 15:50:11 +00:00
|
|
|
//! The WIP stable interface to rustc internals.
|
2023-03-03 17:08:49 -08:00
|
|
|
//!
|
2023-09-14 15:50:11 +00:00
|
|
|
//! For more information see <https://github.com/rust-lang/project-stable-mir>
|
2023-03-03 17:08:49 -08:00
|
|
|
//!
|
2023-09-14 15:50:11 +00:00
|
|
|
//! # Note
|
|
|
|
//!
|
|
|
|
//! This API is still completely unstable and subject to change.
|
|
|
|
|
|
|
|
#![doc(
|
|
|
|
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
|
|
|
|
test(attr(allow(unused_variables), deny(warnings)))
|
|
|
|
)]
|
2023-03-03 17:08:49 -08:00
|
|
|
//!
|
2023-09-14 15:50:11 +00:00
|
|
|
//! This crate shall contain all type definitions and APIs that we expect third-party tools to invoke to
|
|
|
|
//! interact with the compiler.
|
2023-03-03 17:08:49 -08:00
|
|
|
//!
|
2023-09-14 15:50:11 +00:00
|
|
|
//! The goal is to eventually be published on
|
|
|
|
//! [crates.io](https://crates.io).
|
2023-03-03 17:08:49 -08:00
|
|
|
|
2023-10-13 22:40:35 -07:00
|
|
|
use crate::mir::mono::InstanceDef;
|
|
|
|
use crate::mir::Body;
|
2023-09-04 13:06:36 +03:00
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::Debug;
|
2023-11-15 10:12:17 +03:00
|
|
|
use std::{cell::Cell, io};
|
2023-04-24 00:47:01 +00:00
|
|
|
|
2023-08-22 01:09:10 -04:00
|
|
|
use self::ty::{
|
2023-10-11 12:44:59 +03:00
|
|
|
GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, LineInfo, Span, TraitDecl,
|
|
|
|
TraitDef, Ty, TyKind,
|
2023-08-22 01:09:10 -04:00
|
|
|
};
|
2023-04-24 00:47:01 +00:00
|
|
|
|
2023-09-14 15:50:11 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate scoped_tls;
|
|
|
|
|
2023-11-16 06:24:53 -08:00
|
|
|
#[macro_use]
|
2023-10-13 22:40:35 -07:00
|
|
|
pub mod error;
|
2023-03-16 16:06:12 +00:00
|
|
|
pub mod mir;
|
2023-04-24 00:47:01 +00:00
|
|
|
pub mod ty;
|
2023-09-01 16:32:22 +00:00
|
|
|
pub mod visitor;
|
2023-03-16 16:06:12 +00:00
|
|
|
|
2023-11-15 10:12:17 +03:00
|
|
|
use crate::mir::pretty::function_name;
|
|
|
|
use crate::mir::Mutability;
|
2023-11-16 06:24:53 -08:00
|
|
|
use crate::ty::{AdtDef, AdtKind, ClosureDef, ClosureKind, Const, RigidTy};
|
2023-10-13 22:40:35 -07:00
|
|
|
pub use error::*;
|
|
|
|
use mir::mono::Instance;
|
2023-11-16 06:24:53 -08:00
|
|
|
use ty::{FnDef, GenericArgs};
|
2023-10-13 22:40:35 -07: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.
|
2023-11-07 14:07:32 -08:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
2023-10-10 11:28:45 +03:00
|
|
|
pub struct DefId(usize);
|
2023-03-03 17:08:49 -08:00
|
|
|
|
2023-09-04 13:06:36 +03:00
|
|
|
impl Debug for DefId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-09-14 15:50:11 +00:00
|
|
|
f.debug_struct("DefId")
|
2023-09-04 13:06:36 +03:00
|
|
|
.field("id", &self.0)
|
|
|
|
.field("name", &with(|cx| cx.name_of_def_id(*self)))
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 11:28:45 +03:00
|
|
|
impl IndexedVal for DefId {
|
|
|
|
fn to_val(index: usize) -> Self {
|
|
|
|
DefId(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_index(&self) -> usize {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 01:53:06 -04:00
|
|
|
/// A unique identification number for each provenance
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
2023-10-10 11:28:45 +03:00
|
|
|
pub struct AllocId(usize);
|
|
|
|
|
|
|
|
impl IndexedVal for AllocId {
|
|
|
|
fn to_val(index: usize) -> Self {
|
|
|
|
AllocId(index)
|
|
|
|
}
|
|
|
|
fn to_index(&self) -> usize {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
2023-09-02 01:53:06 -04:00
|
|
|
|
2023-03-03 17:08:49 -08:00
|
|
|
/// 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-03-03 17:08:49 -08:00
|
|
|
/// Holds information about a crate.
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct Crate {
|
2023-09-14 15:50:11 +00:00
|
|
|
pub id: CrateNum,
|
2023-03-03 17:08:49 -08:00
|
|
|
pub name: Symbol,
|
|
|
|
pub is_local: bool,
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:01:10 -08:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
|
|
|
pub enum ItemKind {
|
|
|
|
Fn,
|
|
|
|
Static,
|
|
|
|
Const,
|
|
|
|
}
|
|
|
|
|
2023-11-21 16:56:19 +03:00
|
|
|
pub type Filename = String;
|
2023-09-14 15:43:30 +00:00
|
|
|
|
2023-03-03 17:08:49 -08:00
|
|
|
/// Holds information about an item in the crate.
|
2023-10-13 22:40:35 -07:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2023-09-14 15:50:11 +00:00
|
|
|
pub struct CrateItem(pub 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-09-04 15:18:42 +00:00
|
|
|
with(|cx| cx.mir_body(self.0))
|
2023-03-16 16:06:12 +00:00
|
|
|
}
|
2023-09-09 14:17:36 +03:00
|
|
|
|
2023-09-12 11:50:50 +03:00
|
|
|
pub fn span(&self) -> Span {
|
2023-09-09 14:17:36 +03:00
|
|
|
with(|cx| cx.span_of_an_item(self.0))
|
|
|
|
}
|
2023-09-18 11:02:26 +00:00
|
|
|
|
|
|
|
pub fn name(&self) -> String {
|
|
|
|
with(|cx| cx.name_of_def_id(self.0))
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:01:10 -08:00
|
|
|
pub fn kind(&self) -> ItemKind {
|
|
|
|
with(|cx| cx.item_kind(*self))
|
2023-09-18 11:02:26 +00:00
|
|
|
}
|
2023-10-13 22:40:35 -07:00
|
|
|
|
|
|
|
pub fn requires_monomorphization(&self) -> bool {
|
|
|
|
with(|cx| cx.requires_monomorphization(self.0))
|
|
|
|
}
|
2023-11-16 12:01:10 -08:00
|
|
|
|
|
|
|
pub fn ty(&self) -> Ty {
|
|
|
|
with(|cx| cx.def_ty(self.0))
|
|
|
|
}
|
2023-11-17 14:03:54 +03:00
|
|
|
|
2023-11-15 10:12:17 +03:00
|
|
|
pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
|
|
|
|
writeln!(w, "{}", function_name(*self))?;
|
|
|
|
self.body().dump(w)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-10-08 22:55:16 +03:00
|
|
|
/// Try to find a crate or crates if multiple crates exist from given name.
|
|
|
|
pub fn find_crates(name: &str) -> Vec<Crate> {
|
|
|
|
with(|cx| cx.find_crates(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 {
|
2023-10-20 23:05:38 -07:00
|
|
|
fn entry_fn(&self) -> Option<CrateItem>;
|
2023-04-24 00:47:01 +00:00
|
|
|
/// Retrieve all items of the local crate that have a MIR associated with them.
|
2023-10-20 23:05:38 -07:00
|
|
|
fn all_local_items(&self) -> CrateItems;
|
|
|
|
fn mir_body(&self, item: DefId) -> mir::Body;
|
|
|
|
fn all_trait_decls(&self) -> TraitDecls;
|
|
|
|
fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl;
|
|
|
|
fn all_trait_impls(&self) -> ImplTraitDecls;
|
|
|
|
fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait;
|
|
|
|
fn generics_of(&self, def_id: DefId) -> Generics;
|
|
|
|
fn predicates_of(&self, def_id: DefId) -> GenericPredicates;
|
|
|
|
fn explicit_predicates_of(&self, def_id: DefId) -> 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.
|
2023-10-08 22:55:16 +03:00
|
|
|
fn find_crates(&self, name: &str) -> Vec<Crate>;
|
2023-04-24 00:47:01 +00:00
|
|
|
|
2023-10-11 12:44:59 +03:00
|
|
|
/// Returns the name of given `DefId`
|
2023-09-04 13:06:36 +03:00
|
|
|
fn name_of_def_id(&self, def_id: DefId) -> String;
|
|
|
|
|
2023-10-11 12:44:59 +03:00
|
|
|
/// Returns printable, human readable form of `Span`
|
2023-10-13 11:44:38 +03:00
|
|
|
fn span_to_string(&self, span: Span) -> String;
|
2023-09-18 11:02:26 +00:00
|
|
|
|
2023-10-11 12:44:59 +03:00
|
|
|
/// Return filename from given `Span`, for diagnostic purposes
|
|
|
|
fn get_filename(&self, span: &Span) -> Filename;
|
|
|
|
|
|
|
|
/// Return lines corresponding to this `Span`
|
2023-10-13 11:44:38 +03:00
|
|
|
fn get_lines(&self, span: &Span) -> LineInfo;
|
2023-10-11 12:44:59 +03:00
|
|
|
|
|
|
|
/// Returns the `kind` of given `DefId`
|
2023-11-16 12:01:10 -08:00
|
|
|
fn item_kind(&self, item: CrateItem) -> ItemKind;
|
|
|
|
|
2023-11-16 06:24:53 -08:00
|
|
|
/// Returns whether this is a foreign item.
|
|
|
|
fn is_foreign_item(&self, item: CrateItem) -> bool;
|
|
|
|
|
2023-11-16 12:01:10 -08:00
|
|
|
/// Returns the kind of a given algebraic data type
|
|
|
|
fn adt_kind(&self, def: AdtDef) -> AdtKind;
|
|
|
|
|
2023-11-16 06:24:53 -08:00
|
|
|
/// Returns if the ADT is a box.
|
|
|
|
fn adt_is_box(&self, def: AdtDef) -> bool;
|
|
|
|
|
|
|
|
/// Evaluate constant as a target usize.
|
|
|
|
fn eval_target_usize(&self, cnst: &Const) -> Result<u64, Error>;
|
|
|
|
|
|
|
|
/// Create a target usize constant for the given value.
|
|
|
|
fn usize_to_const(&self, val: u64) -> Result<Const, Error>;
|
|
|
|
|
|
|
|
/// Create a new type from the given kind.
|
|
|
|
fn new_rigid_ty(&self, kind: RigidTy) -> Ty;
|
|
|
|
|
2023-11-16 12:01:10 -08:00
|
|
|
/// Returns the type of given crate item.
|
|
|
|
fn def_ty(&self, item: DefId) -> Ty;
|
2023-09-14 15:50:11 +00:00
|
|
|
|
2023-11-17 14:03:54 +03:00
|
|
|
/// Returns literal value of a const as a string.
|
|
|
|
fn const_literal(&self, cnst: &Const) -> String;
|
|
|
|
|
2023-09-09 14:17:36 +03:00
|
|
|
/// `Span` of an item
|
2023-10-20 23:05:38 -07:00
|
|
|
fn span_of_an_item(&self, def_id: DefId) -> Span;
|
2023-09-09 14:17:36 +03:00
|
|
|
|
2023-04-24 00:47:01 +00:00
|
|
|
/// Obtain the representation of a type.
|
2023-10-20 23:05:38 -07:00
|
|
|
fn ty_kind(&self, ty: Ty) -> TyKind;
|
2023-04-24 00:47:01 +00:00
|
|
|
|
2023-10-13 22:40:35 -07:00
|
|
|
/// Get the body of an Instance.
|
|
|
|
/// FIXME: Monomorphize the body.
|
2023-11-16 12:01:10 -08:00
|
|
|
fn instance_body(&self, instance: InstanceDef) -> Option<Body>;
|
2023-10-13 22:40:35 -07:00
|
|
|
|
|
|
|
/// Get the instance type with generic substitutions applied and lifetimes erased.
|
2023-10-20 23:05:38 -07:00
|
|
|
fn instance_ty(&self, instance: InstanceDef) -> Ty;
|
2023-10-13 22:40:35 -07:00
|
|
|
|
|
|
|
/// Get the instance.
|
2023-10-20 23:05:38 -07:00
|
|
|
fn instance_def_id(&self, instance: InstanceDef) -> DefId;
|
2023-10-13 22:40:35 -07:00
|
|
|
|
2023-10-30 13:07:02 -07:00
|
|
|
/// Get the instance mangled name.
|
|
|
|
fn instance_mangled_name(&self, instance: InstanceDef) -> String;
|
|
|
|
|
2023-10-13 22:40:35 -07:00
|
|
|
/// Convert a non-generic crate item into an instance.
|
|
|
|
/// This function will panic if the item is generic.
|
2023-10-20 23:05:38 -07:00
|
|
|
fn mono_instance(&self, item: CrateItem) -> Instance;
|
2023-10-13 22:40:35 -07:00
|
|
|
|
|
|
|
/// Item requires monomorphization.
|
|
|
|
fn requires_monomorphization(&self, def_id: DefId) -> bool;
|
2023-10-19 17:06:53 -07:00
|
|
|
|
|
|
|
/// Resolve an instance from the given function definition and generic arguments.
|
2023-10-20 23:05:38 -07:00
|
|
|
fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
|
2023-11-16 12:01:10 -08:00
|
|
|
|
|
|
|
/// Resolve an instance for drop_in_place for the given type.
|
2023-11-16 12:04:25 -08:00
|
|
|
fn resolve_drop_in_place(&self, ty: Ty) -> Instance;
|
2023-11-16 12:01:10 -08:00
|
|
|
|
|
|
|
/// Resolve instance for a function pointer.
|
|
|
|
fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
|
|
|
|
|
|
|
|
/// Resolve instance for a closure with the requested type.
|
|
|
|
fn resolve_closure(
|
|
|
|
&self,
|
|
|
|
def: ClosureDef,
|
|
|
|
args: &GenericArgs,
|
|
|
|
kind: ClosureKind,
|
|
|
|
) -> Option<Instance>;
|
2023-04-24 00:47:01 +00:00
|
|
|
}
|
|
|
|
|
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
|
2023-10-20 23:05:38 -07:00
|
|
|
scoped_thread_local! (static TLV: Cell<*const ()>);
|
2023-04-24 00:47:01 +00:00
|
|
|
|
2023-11-07 14:07:32 -08:00
|
|
|
pub fn run<F, T>(context: &dyn Context, f: F) -> Result<T, Error>
|
|
|
|
where
|
|
|
|
F: FnOnce() -> T,
|
|
|
|
{
|
|
|
|
if TLV.is_set() {
|
|
|
|
Err(Error::from("StableMIR already running"))
|
|
|
|
} else {
|
|
|
|
let ptr: *const () = &context as *const &_ as _;
|
|
|
|
TLV.set(&Cell::new(ptr), || Ok(f()))
|
|
|
|
}
|
2023-04-24 00:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads the current context and calls a function with it.
|
|
|
|
/// Do not nest these, as that will ICE.
|
2023-10-20 23:05:38 -07:00
|
|
|
pub fn with<R>(f: impl FnOnce(&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());
|
2023-10-20 23:05:38 -07:00
|
|
|
f(unsafe { *(ptr as *const &dyn Context) })
|
2023-07-02 07:48:41 +08:00
|
|
|
})
|
2023-03-07 12:47:25 -08:00
|
|
|
}
|
2023-09-18 14:47:39 +00:00
|
|
|
|
|
|
|
/// A type that provides internal information but that can still be used for debug purpose.
|
2023-11-07 14:07:32 -08:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2023-09-18 14:47:39 +00:00
|
|
|
pub struct Opaque(String);
|
|
|
|
|
|
|
|
impl std::fmt::Display for Opaque {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Opaque {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{:?}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 15:50:11 +00:00
|
|
|
pub fn opaque<T: Debug>(value: &T) -> Opaque {
|
2023-09-18 14:47:39 +00:00
|
|
|
Opaque(format!("{value:?}"))
|
|
|
|
}
|