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-11-24 15:09:26 -08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate scoped_tls;
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use crate::compiler_interface::with;
|
2023-11-24 13:41:45 -08:00
|
|
|
pub use crate::crate_def::CrateDef;
|
|
|
|
pub use crate::crate_def::DefId;
|
2023-11-24 15:09:26 -08:00
|
|
|
pub use crate::error::*;
|
2023-11-24 13:41:45 -08:00
|
|
|
use crate::mir::pretty::function_name;
|
2023-10-13 22:40:35 -07:00
|
|
|
use crate::mir::Body;
|
2023-11-24 13:41:45 -08:00
|
|
|
use crate::mir::Mutability;
|
2024-02-12 19:44:35 +00:00
|
|
|
use crate::ty::{ForeignModuleDef, ImplDef, IndexedVal, Span, TraitDef, Ty};
|
2023-09-14 15:50:11 +00:00
|
|
|
|
2023-12-18 19:52:25 +00:00
|
|
|
pub mod abi;
|
2023-11-21 13:41:02 -08:00
|
|
|
#[macro_use]
|
|
|
|
pub mod crate_def;
|
2023-11-24 13:41:45 -08:00
|
|
|
pub mod compiler_interface;
|
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-12-06 13:39:55 -08:00
|
|
|
pub mod target;
|
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-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;
|
|
|
|
|
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)
|
2023-11-21 13:41:02 -08:00
|
|
|
.field("name", &with(|cx| cx.def_name(*self, false)))
|
2023-09-04 13:06:36 +03:00
|
|
|
.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-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,
|
|
|
|
}
|
|
|
|
|
2024-01-09 15:45:03 -08:00
|
|
|
impl Crate {
|
2024-02-12 19:44:35 +00:00
|
|
|
/// The list of foreign modules in this crate.
|
|
|
|
pub fn foreign_modules(&self) -> Vec<ForeignModuleDef> {
|
|
|
|
with(|cx| cx.foreign_modules(self.id))
|
|
|
|
}
|
|
|
|
|
2024-01-09 15:45:03 -08:00
|
|
|
/// The list of traits declared in this crate.
|
|
|
|
pub fn trait_decls(&self) -> TraitDecls {
|
|
|
|
with(|cx| cx.trait_decls(self.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The list of trait implementations in this crate.
|
|
|
|
pub fn trait_impls(&self) -> ImplTraitDecls {
|
|
|
|
with(|cx| cx.trait_impls(self.id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:01:10 -08:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
|
|
|
pub enum ItemKind {
|
|
|
|
Fn,
|
|
|
|
Static,
|
|
|
|
Const,
|
2023-12-19 15:03:57 -08:00
|
|
|
Ctor(CtorKind),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
|
|
|
pub enum CtorKind {
|
|
|
|
Const,
|
|
|
|
Fn,
|
2023-11-16 12:01:10 -08:00
|
|
|
}
|
|
|
|
|
2023-11-21 16:56:19 +03:00
|
|
|
pub type Filename = String;
|
2023-09-14 15:43:30 +00:00
|
|
|
|
2023-11-21 13:41:02 -08:00
|
|
|
crate_def! {
|
|
|
|
/// Holds information about an item in a crate.
|
|
|
|
pub CrateItem;
|
|
|
|
}
|
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
|
|
|
|
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-07 14:12:58 -08:00
|
|
|
pub fn is_foreign_item(&self) -> bool {
|
2023-12-06 10:48:18 -08:00
|
|
|
with(|cx| cx.is_foreign_item(self.0))
|
2023-11-07 14:12:58 -08: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 all_trait_impls() -> ImplTraitDecls {
|
|
|
|
with(|cx| cx.all_trait_impls())
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-11-22 15:05:32 +03:00
|
|
|
write!(f, "{}", self.0)
|
2023-09-18 14:47:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:?}"))
|
|
|
|
}
|