|
17 | 17 | //! The goal is to eventually be published on |
18 | 18 | //! [crates.io](https://crates.io). |
19 | 19 |
|
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; |
21 | 25 | use crate::mir::Body; |
| 26 | +use crate::mir::Mutability; |
| 27 | +pub use error::*; |
22 | 28 | use std::fmt; |
23 | 29 | 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; |
30 | 31 |
|
31 | 32 | #[macro_use] |
32 | 33 | extern crate scoped_tls; |
33 | 34 |
|
34 | 35 | #[macro_use] |
35 | 36 | pub mod crate_def; |
| 37 | +pub mod compiler_interface; |
36 | 38 | #[macro_use] |
37 | 39 | pub mod error; |
38 | 40 | pub mod mir; |
39 | 41 | pub mod ty; |
40 | 42 | pub mod visitor; |
41 | 43 |
|
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 | | - |
52 | 44 | /// Use String for now but we should replace it. |
53 | 45 | pub type Symbol = String; |
54 | 46 |
|
@@ -179,149 +171,6 @@ pub fn trait_impl(trait_impl: &ImplDef) -> ImplTrait { |
179 | 171 | with(|cx| cx.trait_impl(trait_impl)) |
180 | 172 | } |
181 | 173 |
|
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 | | - |
325 | 174 | /// A type that provides internal information but that can still be used for debug purpose. |
326 | 175 | #[derive(Clone, PartialEq, Eq, Hash)] |
327 | 176 | pub struct Opaque(String); |
|
0 commit comments