Skip to content

Commit d7c7236

Browse files
committed
Move the compiler interface defs to its own module
Separate items that are exposed in the `stable_mir` crate to be used by the compiler from items that we expect to be used by tool developers.
1 parent 5ad84ed commit d7c7236

File tree

4 files changed

+176
-163
lines changed

4 files changed

+176
-163
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ where
181181
instances: IndexMap::default(),
182182
constants: IndexMap::default(),
183183
}));
184-
stable_mir::run(&tables, || init(&tables, f))
184+
stable_mir::compiler_interface::run(&tables, || init(&tables, f))
185185
}
186186

187187
#[macro_export]

compiler/rustc_smir/src/rustc_smir/context.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
//! Implementation of `[stable_mir::compiler::Context]` trait.
1+
//! Implementation of `[stable_mir::compiler_interface::Context]` trait.
22
//!
33
//! This trait is currently the main interface between the Rust compiler,
44
//! and the `stable_mir` crate.
55
66
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
77
use rustc_middle::ty::{GenericPredicates, Instance, ParamEnv, ScalarInt, ValTree};
88
use rustc_span::def_id::LOCAL_CRATE;
9+
use stable_mir::compiler_interface::Context;
910
use stable_mir::mir::alloc::GlobalAlloc;
1011
use stable_mir::mir::mono::{InstanceDef, StaticDef};
1112
use stable_mir::mir::Body;
1213
use stable_mir::ty::{
1314
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FnDef, GenericArgs, LineInfo,
1415
RigidTy, Span, TyKind,
1516
};
16-
use stable_mir::{self, Context, Crate, CrateItem, Error, Filename, ItemKind, Symbol};
17+
use stable_mir::{self, Crate, CrateItem, Error, Filename, ItemKind, Symbol};
1718
use std::cell::RefCell;
1819

1920
use crate::rustc_internal::{internal, RustcInternal};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//! Define the interface with the Rust compiler.
2+
//!
3+
//! StableMIR users should not use any of the items in this module directly.
4+
//! These APIs have no stability guarantee.
5+
6+
use crate::mir::alloc::{AllocId, GlobalAlloc};
7+
use crate::mir::mono::{Instance, InstanceDef, StaticDef};
8+
use crate::mir::Body;
9+
use crate::ty::{
10+
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FnDef, GenericArgs,
11+
GenericPredicates, Generics, ImplDef, ImplTrait, LineInfo, RigidTy, Span, TraitDecl, TraitDef,
12+
Ty, TyKind,
13+
};
14+
use crate::{
15+
mir, Crate, CrateItem, CrateItems, DefId, Error, Filename, ImplTraitDecls, ItemKind, Symbol,
16+
TraitDecls,
17+
};
18+
use std::cell::Cell;
19+
20+
/// This trait defines the interface between stable_mir and the Rust compiler.
21+
/// Do not use this directly.
22+
pub trait Context {
23+
fn entry_fn(&self) -> Option<CrateItem>;
24+
/// Retrieve all items of the local crate that have a MIR associated with them.
25+
fn all_local_items(&self) -> CrateItems;
26+
fn mir_body(&self, item: DefId) -> mir::Body;
27+
fn all_trait_decls(&self) -> TraitDecls;
28+
fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl;
29+
fn all_trait_impls(&self) -> ImplTraitDecls;
30+
fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait;
31+
fn generics_of(&self, def_id: DefId) -> Generics;
32+
fn predicates_of(&self, def_id: DefId) -> GenericPredicates;
33+
fn explicit_predicates_of(&self, def_id: DefId) -> GenericPredicates;
34+
/// Get information about the local crate.
35+
fn local_crate(&self) -> Crate;
36+
/// Retrieve a list of all external crates.
37+
fn external_crates(&self) -> Vec<Crate>;
38+
39+
/// Find a crate with the given name.
40+
fn find_crates(&self, name: &str) -> Vec<Crate>;
41+
42+
/// Returns the name of given `DefId`
43+
fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol;
44+
45+
/// Returns printable, human readable form of `Span`
46+
fn span_to_string(&self, span: Span) -> String;
47+
48+
/// Return filename from given `Span`, for diagnostic purposes
49+
fn get_filename(&self, span: &Span) -> Filename;
50+
51+
/// Return lines corresponding to this `Span`
52+
fn get_lines(&self, span: &Span) -> LineInfo;
53+
54+
/// Returns the `kind` of given `DefId`
55+
fn item_kind(&self, item: CrateItem) -> ItemKind;
56+
57+
/// Returns whether this is a foreign item.
58+
fn is_foreign_item(&self, item: CrateItem) -> bool;
59+
60+
/// Returns the kind of a given algebraic data type
61+
fn adt_kind(&self, def: AdtDef) -> AdtKind;
62+
63+
/// Returns if the ADT is a box.
64+
fn adt_is_box(&self, def: AdtDef) -> bool;
65+
66+
/// Evaluate constant as a target usize.
67+
fn eval_target_usize(&self, cnst: &Const) -> Result<u64, Error>;
68+
69+
/// Create a target usize constant for the given value.
70+
fn usize_to_const(&self, val: u64) -> Result<Const, Error>;
71+
72+
/// Create a new type from the given kind.
73+
fn new_rigid_ty(&self, kind: RigidTy) -> Ty;
74+
75+
/// Returns the type of given crate item.
76+
fn def_ty(&self, item: DefId) -> Ty;
77+
78+
/// Returns literal value of a const as a string.
79+
fn const_literal(&self, cnst: &Const) -> String;
80+
81+
/// `Span` of an item
82+
fn span_of_an_item(&self, def_id: DefId) -> Span;
83+
84+
/// Obtain the representation of a type.
85+
fn ty_kind(&self, ty: Ty) -> TyKind;
86+
87+
/// Get the body of an Instance.
88+
/// FIXME: Monomorphize the body.
89+
fn instance_body(&self, instance: InstanceDef) -> Option<Body>;
90+
91+
/// Get the instance type with generic substitutions applied and lifetimes erased.
92+
fn instance_ty(&self, instance: InstanceDef) -> Ty;
93+
94+
/// Get the instance.
95+
fn instance_def_id(&self, instance: InstanceDef) -> DefId;
96+
97+
/// Get the instance mangled name.
98+
fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol;
99+
100+
/// Convert a non-generic crate item into an instance.
101+
/// This function will panic if the item is generic.
102+
fn mono_instance(&self, item: CrateItem) -> Instance;
103+
104+
/// Item requires monomorphization.
105+
fn requires_monomorphization(&self, def_id: DefId) -> bool;
106+
107+
/// Resolve an instance from the given function definition and generic arguments.
108+
fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
109+
110+
/// Resolve an instance for drop_in_place for the given type.
111+
fn resolve_drop_in_place(&self, ty: Ty) -> Instance;
112+
113+
/// Resolve instance for a function pointer.
114+
fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
115+
116+
/// Resolve instance for a closure with the requested type.
117+
fn resolve_closure(
118+
&self,
119+
def: ClosureDef,
120+
args: &GenericArgs,
121+
kind: ClosureKind,
122+
) -> Option<Instance>;
123+
124+
/// Evaluate a static's initializer.
125+
fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error>;
126+
127+
/// Retrieve global allocation for the given allocation ID.
128+
fn global_alloc(&self, id: AllocId) -> GlobalAlloc;
129+
130+
/// Retrieve the id for the virtual table.
131+
fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId>;
132+
fn krate(&self, def_id: DefId) -> Crate;
133+
fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol;
134+
}
135+
136+
// A thread local variable that stores a pointer to the tables mapping between TyCtxt
137+
// datastructures and stable MIR datastructures
138+
scoped_thread_local! (static TLV: Cell<*const ()>);
139+
140+
pub fn run<F, T>(context: &dyn Context, f: F) -> Result<T, Error>
141+
where
142+
F: FnOnce() -> T,
143+
{
144+
if TLV.is_set() {
145+
Err(Error::from("StableMIR already running"))
146+
} else {
147+
let ptr: *const () = &context as *const &_ as _;
148+
TLV.set(&Cell::new(ptr), || Ok(f()))
149+
}
150+
}
151+
152+
/// Execute the given function with access the compiler [Context].
153+
///
154+
/// I.e., This function will load the current context and calls a function with it.
155+
/// Do not nest these, as that will ICE.
156+
pub(crate) fn with<R>(f: impl FnOnce(&dyn Context) -> R) -> R {
157+
assert!(TLV.is_set());
158+
TLV.with(|tlv| {
159+
let ptr = tlv.get();
160+
assert!(!ptr.is_null());
161+
f(unsafe { *(ptr as *const &dyn Context) })
162+
})
163+
}

compiler/stable_mir/src/lib.rs

+9-160
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,30 @@
1717
//! The goal is to eventually be published on
1818
//! [crates.io](https://crates.io).
1919
20-
use crate::mir::mono::{InstanceDef, StaticDef};
20+
use self::ty::{ImplDef, ImplTrait, IndexedVal, Span, TraitDecl, TraitDef, Ty};
21+
pub(crate) use crate::compiler_interface::with;
22+
pub use crate::crate_def::CrateDef;
23+
pub use crate::crate_def::DefId;
24+
use crate::mir::pretty::function_name;
2125
use crate::mir::Body;
26+
use crate::mir::Mutability;
27+
pub use error::*;
2228
use std::fmt;
2329
use std::fmt::Debug;
24-
use std::{cell::Cell, io};
25-
26-
use self::ty::{
27-
GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, LineInfo, Span, TraitDecl,
28-
TraitDef, Ty, TyKind,
29-
};
30+
use std::io;
3031

3132
#[macro_use]
3233
extern crate scoped_tls;
3334

3435
#[macro_use]
3536
pub mod crate_def;
37+
pub mod compiler_interface;
3638
#[macro_use]
3739
pub mod error;
3840
pub mod mir;
3941
pub mod ty;
4042
pub mod visitor;
4143

42-
pub use crate::crate_def::CrateDef;
43-
pub use crate::crate_def::DefId;
44-
use crate::mir::alloc::{AllocId, GlobalAlloc};
45-
use crate::mir::pretty::function_name;
46-
use crate::mir::Mutability;
47-
use crate::ty::{AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, RigidTy};
48-
pub use error::*;
49-
use mir::mono::Instance;
50-
use ty::{FnDef, GenericArgs};
51-
5244
/// Use String for now but we should replace it.
5345
pub type Symbol = String;
5446

@@ -179,149 +171,6 @@ pub fn trait_impl(trait_impl: &ImplDef) -> ImplTrait {
179171
with(|cx| cx.trait_impl(trait_impl))
180172
}
181173

182-
/// This trait defines the interface between stable_mir and the Rust compiler.
183-
/// Do not use this directly.
184-
pub trait Context {
185-
fn entry_fn(&self) -> Option<CrateItem>;
186-
/// Retrieve all items of the local crate that have a MIR associated with them.
187-
fn all_local_items(&self) -> CrateItems;
188-
fn mir_body(&self, item: DefId) -> mir::Body;
189-
fn all_trait_decls(&self) -> TraitDecls;
190-
fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl;
191-
fn all_trait_impls(&self) -> ImplTraitDecls;
192-
fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait;
193-
fn generics_of(&self, def_id: DefId) -> Generics;
194-
fn predicates_of(&self, def_id: DefId) -> GenericPredicates;
195-
fn explicit_predicates_of(&self, def_id: DefId) -> GenericPredicates;
196-
/// Get information about the local crate.
197-
fn local_crate(&self) -> Crate;
198-
/// Retrieve a list of all external crates.
199-
fn external_crates(&self) -> Vec<Crate>;
200-
201-
/// Find a crate with the given name.
202-
fn find_crates(&self, name: &str) -> Vec<Crate>;
203-
204-
/// Returns the name of given `DefId`
205-
fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol;
206-
207-
/// Returns printable, human readable form of `Span`
208-
fn span_to_string(&self, span: Span) -> String;
209-
210-
/// Return filename from given `Span`, for diagnostic purposes
211-
fn get_filename(&self, span: &Span) -> Filename;
212-
213-
/// Return lines corresponding to this `Span`
214-
fn get_lines(&self, span: &Span) -> LineInfo;
215-
216-
/// Returns the `kind` of given `DefId`
217-
fn item_kind(&self, item: CrateItem) -> ItemKind;
218-
219-
/// Returns whether this is a foreign item.
220-
fn is_foreign_item(&self, item: CrateItem) -> bool;
221-
222-
/// Returns the kind of a given algebraic data type
223-
fn adt_kind(&self, def: AdtDef) -> AdtKind;
224-
225-
/// Returns if the ADT is a box.
226-
fn adt_is_box(&self, def: AdtDef) -> bool;
227-
228-
/// Evaluate constant as a target usize.
229-
fn eval_target_usize(&self, cnst: &Const) -> Result<u64, Error>;
230-
231-
/// Create a target usize constant for the given value.
232-
fn usize_to_const(&self, val: u64) -> Result<Const, Error>;
233-
234-
/// Create a new type from the given kind.
235-
fn new_rigid_ty(&self, kind: RigidTy) -> Ty;
236-
237-
/// Returns the type of given crate item.
238-
fn def_ty(&self, item: DefId) -> Ty;
239-
240-
/// Returns literal value of a const as a string.
241-
fn const_literal(&self, cnst: &Const) -> String;
242-
243-
/// `Span` of an item
244-
fn span_of_an_item(&self, def_id: DefId) -> Span;
245-
246-
/// Obtain the representation of a type.
247-
fn ty_kind(&self, ty: Ty) -> TyKind;
248-
249-
/// Get the body of an Instance.
250-
/// FIXME: Monomorphize the body.
251-
fn instance_body(&self, instance: InstanceDef) -> Option<Body>;
252-
253-
/// Get the instance type with generic substitutions applied and lifetimes erased.
254-
fn instance_ty(&self, instance: InstanceDef) -> Ty;
255-
256-
/// Get the instance.
257-
fn instance_def_id(&self, instance: InstanceDef) -> DefId;
258-
259-
/// Get the instance mangled name.
260-
fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol;
261-
262-
/// Convert a non-generic crate item into an instance.
263-
/// This function will panic if the item is generic.
264-
fn mono_instance(&self, item: CrateItem) -> Instance;
265-
266-
/// Item requires monomorphization.
267-
fn requires_monomorphization(&self, def_id: DefId) -> bool;
268-
269-
/// Resolve an instance from the given function definition and generic arguments.
270-
fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
271-
272-
/// Resolve an instance for drop_in_place for the given type.
273-
fn resolve_drop_in_place(&self, ty: Ty) -> Instance;
274-
275-
/// Resolve instance for a function pointer.
276-
fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
277-
278-
/// Resolve instance for a closure with the requested type.
279-
fn resolve_closure(
280-
&self,
281-
def: ClosureDef,
282-
args: &GenericArgs,
283-
kind: ClosureKind,
284-
) -> Option<Instance>;
285-
286-
/// Evaluate a static's initializer.
287-
fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error>;
288-
289-
/// Retrieve global allocation for the given allocation ID.
290-
fn global_alloc(&self, id: AllocId) -> GlobalAlloc;
291-
292-
/// Retrieve the id for the virtual table.
293-
fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId>;
294-
fn krate(&self, def_id: DefId) -> Crate;
295-
fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol;
296-
}
297-
298-
// A thread local variable that stores a pointer to the tables mapping between TyCtxt
299-
// datastructures and stable MIR datastructures
300-
scoped_thread_local! (static TLV: Cell<*const ()>);
301-
302-
pub fn run<F, T>(context: &dyn Context, f: F) -> Result<T, Error>
303-
where
304-
F: FnOnce() -> T,
305-
{
306-
if TLV.is_set() {
307-
Err(Error::from("StableMIR already running"))
308-
} else {
309-
let ptr: *const () = &context as *const &_ as _;
310-
TLV.set(&Cell::new(ptr), || Ok(f()))
311-
}
312-
}
313-
314-
/// Loads the current context and calls a function with it.
315-
/// Do not nest these, as that will ICE.
316-
pub fn with<R>(f: impl FnOnce(&dyn Context) -> R) -> R {
317-
assert!(TLV.is_set());
318-
TLV.with(|tlv| {
319-
let ptr = tlv.get();
320-
assert!(!ptr.is_null());
321-
f(unsafe { *(ptr as *const &dyn Context) })
322-
})
323-
}
324-
325174
/// A type that provides internal information but that can still be used for debug purpose.
326175
#[derive(Clone, PartialEq, Eq, Hash)]
327176
pub struct Opaque(String);

0 commit comments

Comments
 (0)