summaryrefslogtreecommitdiffstats
path: root/compiler/stable_mir/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/stable_mir/src/lib.rs')
-rw-r--r--compiler/stable_mir/src/lib.rs186
1 files changed, 54 insertions, 132 deletions
diff --git a/compiler/stable_mir/src/lib.rs b/compiler/stable_mir/src/lib.rs
index f316671b2..9194f1e6b 100644
--- a/compiler/stable_mir/src/lib.rs
+++ b/compiler/stable_mir/src/lib.rs
@@ -16,45 +16,45 @@
//!
//! The goal is to eventually be published on
//! [crates.io](https://crates.io).
+#![feature(type_alias_impl_trait)]
+#[macro_use]
+extern crate scoped_tls;
-use crate::mir::mono::InstanceDef;
-use crate::mir::Body;
-use std::cell::Cell;
use std::fmt;
use std::fmt::Debug;
+use std::io;
-use self::ty::{
- GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, LineInfo, Span, TraitDecl,
- TraitDef, Ty, TyKind,
-};
+use crate::compiler_interface::with;
+pub use crate::crate_def::CrateDef;
+pub use crate::crate_def::DefId;
+pub use crate::error::*;
+use crate::mir::pretty::function_name;
+use crate::mir::Body;
+use crate::mir::Mutability;
+use crate::ty::{ImplDef, ImplTrait, IndexedVal, Span, TraitDecl, TraitDef, Ty};
+pub mod abi;
+#[macro_use]
+pub mod crate_def;
+pub mod compiler_interface;
#[macro_use]
-extern crate scoped_tls;
-
pub mod error;
pub mod mir;
+pub mod target;
pub mod ty;
pub mod visitor;
-pub use error::*;
-use mir::mono::Instance;
-use ty::{FnDef, GenericArgs};
-
/// 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.
-#[derive(Clone, Copy, PartialEq, Eq)]
-pub struct DefId(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)))
+ .field("name", &with(|cx| cx.def_name(*self, false)))
.finish()
}
}
@@ -69,19 +69,6 @@ impl IndexedVal for DefId {
}
}
-/// A unique identification number for each provenance
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub struct AllocId(usize);
-
-impl IndexedVal for AllocId {
- fn to_val(index: usize) -> Self {
- AllocId(index)
- }
- fn to_index(&self) -> usize {
- self.0
- }
-}
-
/// A list of crate items.
pub type CrateItems = Vec<CrateItem>;
@@ -99,12 +86,26 @@ pub struct Crate {
pub is_local: bool,
}
-pub type DefKind = Opaque;
-pub type Filename = Opaque;
+#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
+pub enum ItemKind {
+ Fn,
+ Static,
+ Const,
+ Ctor(CtorKind),
+}
-/// Holds information about an item in the crate.
-#[derive(Copy, Clone, PartialEq, Eq, Debug)]
-pub struct CrateItem(pub DefId);
+#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
+pub enum CtorKind {
+ Const,
+ Fn,
+}
+
+pub type Filename = String;
+
+crate_def! {
+ /// Holds information about an item in a crate.
+ pub CrateItem;
+}
impl CrateItem {
pub fn body(&self) -> mir::Body {
@@ -115,17 +116,26 @@ impl CrateItem {
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 kind(&self) -> ItemKind {
+ with(|cx| cx.item_kind(*self))
}
pub fn requires_monomorphization(&self) -> bool {
with(|cx| cx.requires_monomorphization(self.0))
}
+
+ pub fn ty(&self) -> Ty {
+ with(|cx| cx.def_ty(self.0))
+ }
+
+ pub fn is_foreign_item(&self) -> bool {
+ with(|cx| cx.is_foreign_item(self.0))
+ }
+
+ pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
+ writeln!(w, "{}", function_name(*self))?;
+ self.body().dump(w)
+ }
}
/// Return the function where execution starts if the current
@@ -171,96 +181,8 @@ pub fn trait_impl(trait_impl: &ImplDef) -> ImplTrait {
with(|cx| cx.trait_impl(trait_impl))
}
-pub trait Context {
- fn entry_fn(&self) -> Option<CrateItem>;
- /// Retrieve all items of the local crate that have a MIR associated with them.
- 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;
- /// 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_crates(&self, name: &str) -> Vec<Crate>;
-
- /// Returns the name of given `DefId`
- fn name_of_def_id(&self, def_id: DefId) -> String;
-
- /// Returns printable, human readable form of `Span`
- fn span_to_string(&self, span: Span) -> String;
-
- /// Return filename from given `Span`, for diagnostic purposes
- fn get_filename(&self, span: &Span) -> Filename;
-
- /// Return lines corresponding to this `Span`
- fn get_lines(&self, span: &Span) -> LineInfo;
-
- /// Returns the `kind` of given `DefId`
- fn def_kind(&self, def_id: DefId) -> DefKind;
-
- /// `Span` of an item
- fn span_of_an_item(&self, def_id: DefId) -> Span;
-
- /// Obtain the representation of a type.
- fn ty_kind(&self, ty: Ty) -> TyKind;
-
- /// Get the body of an Instance.
- /// FIXME: Monomorphize the body.
- fn instance_body(&self, instance: InstanceDef) -> Body;
-
- /// Get the instance type with generic substitutions applied and lifetimes erased.
- fn instance_ty(&self, instance: InstanceDef) -> Ty;
-
- /// Get the instance.
- fn instance_def_id(&self, instance: InstanceDef) -> DefId;
-
- /// Get the instance mangled name.
- fn instance_mangled_name(&self, instance: InstanceDef) -> String;
-
- /// Convert a non-generic crate item into an instance.
- /// This function will panic if the item is generic.
- fn mono_instance(&self, item: CrateItem) -> Instance;
-
- /// Item requires monomorphization.
- fn requires_monomorphization(&self, def_id: DefId) -> bool;
-
- /// Resolve an instance from the given function definition and generic arguments.
- fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
-}
-
-// 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<*const ()>);
-
-pub fn run(context: &dyn Context, f: impl FnOnce()) {
- assert!(!TLV.is_set());
- let ptr: *const () = &context as *const &_ as _;
- TLV.set(&Cell::new(ptr), || {
- f();
- });
-}
-
-/// Loads the current context and calls a function with it.
-/// Do not nest these, as that will ICE.
-pub fn with<R>(f: impl FnOnce(&dyn Context) -> R) -> R {
- assert!(TLV.is_set());
- TLV.with(|tlv| {
- let ptr = tlv.get();
- assert!(!ptr.is_null());
- f(unsafe { *(ptr as *const &dyn Context) })
- })
-}
-
/// A type that provides internal information but that can still be used for debug purpose.
-#[derive(Clone, Eq, PartialEq)]
+#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Opaque(String);
impl std::fmt::Display for Opaque {
@@ -271,7 +193,7 @@ impl std::fmt::Display for Opaque {
impl std::fmt::Debug for Opaque {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{:?}", self.0)
+ write!(f, "{}", self.0)
}
}