#![doc(
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
test(attr(allow(unused_variables), deny(warnings)))
)]
use std::cell::Cell;
use std::fmt;
use std::fmt::Debug;
use self::ty::{
GenericPredicates, Generics, ImplDef, ImplTrait, Span, TraitDecl, TraitDef, Ty, TyKind,
};
#[macro_use]
extern crate scoped_tls;
pub mod fold;
pub mod mir;
pub mod ty;
pub mod visitor;
pub type Symbol = String;
pub type CrateNum = usize;
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct DefId(pub usize);
impl Debug for DefId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DefId")
.field("id", &self.0)
.field("name", &with(|cx| cx.name_of_def_id(*self)))
.finish()
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct AllocId(pub usize);
pub type CrateItems = Vec<CrateItem>;
pub type TraitDecls = Vec<TraitDef>;
pub type ImplTraitDecls = Vec<ImplDef>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CompilerError<T> {
ICE,
CompilationFailed,
Interrupted(T),
Skipped,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Crate {
pub id: CrateNum,
pub name: Symbol,
pub is_local: bool,
}
pub type DefKind = Opaque;
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CrateItem(pub DefId);
impl CrateItem {
pub fn body(&self) -> mir::Body {
with(|cx| cx.mir_body(self.0))
}
pub fn span(&self) -> Span {
with(|cx| cx.span_of_an_item(self.0))
}
pub fn name(&self) -> String {
with(|cx| cx.name_of_def_id(self.0))
}
pub fn kind(&self) -> DefKind {
with(|cx| cx.def_kind(self.0))
}
}
pub fn entry_fn() -> Option<CrateItem> {
with(|cx| cx.entry_fn())
}
pub fn local_crate() -> Crate {
with(|cx| cx.local_crate())
}
pub fn find_crate(name: &str) -> Option<Crate> {
with(|cx| cx.find_crate(name))
}
pub fn external_crates() -> Vec<Crate> {
with(|cx| cx.external_crates())
}
pub fn all_local_items() -> CrateItems {
with(|cx| cx.all_local_items())
}
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))
}
pub trait Context {
fn entry_fn(&mut self) -> Option<CrateItem>;
fn all_local_items(&mut self) -> CrateItems;
fn mir_body(&mut self, item: DefId) -> mir::Body;
fn all_trait_decls(&mut self) -> TraitDecls;
fn trait_decl(&mut self, trait_def: &TraitDef) -> TraitDecl;
fn all_trait_impls(&mut self) -> ImplTraitDecls;
fn trait_impl(&mut self, trait_impl: &ImplDef) -> ImplTrait;
fn generics_of(&mut self, def_id: DefId) -> Generics;
fn predicates_of(&mut self, def_id: DefId) -> GenericPredicates;
fn explicit_predicates_of(&mut self, def_id: DefId) -> GenericPredicates;
fn local_crate(&self) -> Crate;
fn external_crates(&self) -> Vec<Crate>;
fn find_crate(&self, name: &str) -> Option<Crate>;
fn name_of_def_id(&self, def_id: DefId) -> String;
fn print_span(&self, span: Span) -> String;
fn def_kind(&mut self, def_id: DefId) -> DefKind;
fn span_of_an_item(&mut self, def_id: DefId) -> Span;
fn ty_kind(&mut self, ty: Ty) -> TyKind;
fn mk_ty(&mut self, kind: TyKind) -> Ty;
}
scoped_thread_local! (static TLV: Cell<*mut ()>);
pub fn run(mut context: impl Context, f: impl FnOnce()) {
assert!(!TLV.is_set());
fn g<'a>(mut context: &mut (dyn Context + 'a), f: impl FnOnce()) {
let ptr: *mut () = &mut context as *mut &mut _ as _;
TLV.set(&Cell::new(ptr), || {
f();
});
}
g(&mut context, f);
}
pub fn with<R>(f: impl FnOnce(&mut dyn Context) -> R) -> R {
assert!(TLV.is_set());
TLV.with(|tlv| {
let ptr = tlv.get();
assert!(!ptr.is_null());
f(unsafe { *(ptr as *mut &mut dyn Context) })
})
}
#[derive(Clone)]
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)
}
}
pub fn opaque<T: Debug>(value: &T) -> Opaque {
Opaque(format!("{value:?}"))
}