rustc_smir/stable_mir/
mod.rs
1use std::fmt::Debug;
28use std::{fmt, io};
29
30use serde::Serialize;
31use stable_mir::compiler_interface::with;
32pub use stable_mir::crate_def::{CrateDef, CrateDefItems, CrateDefType, DefId};
33pub use stable_mir::error::*;
34use stable_mir::mir::mono::StaticDef;
35use stable_mir::mir::{Body, Mutability};
36use stable_mir::ty::{AssocItem, FnDef, ForeignModuleDef, ImplDef, IndexedVal, Span, TraitDef, Ty};
37
38use crate::stable_mir;
39
40pub mod abi;
41#[macro_use]
42pub mod crate_def;
43pub mod compiler_interface;
44#[macro_use]
45pub mod error;
46pub mod mir;
47pub mod target;
48pub mod ty;
49pub mod visitor;
50
51pub type Symbol = String;
53
54pub type CrateNum = usize;
56
57impl Debug for DefId {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 f.debug_struct("DefId").field("id", &self.0).field("name", &self.name()).finish()
60 }
61}
62
63impl IndexedVal for DefId {
64 fn to_val(index: usize) -> Self {
65 DefId(index)
66 }
67
68 fn to_index(&self) -> usize {
69 self.0
70 }
71}
72
73pub type CrateItems = Vec<CrateItem>;
75
76pub type TraitDecls = Vec<TraitDef>;
78
79pub type ImplTraitDecls = Vec<ImplDef>;
81
82pub type AssocItems = Vec<AssocItem>;
84
85#[derive(Clone, PartialEq, Eq, Debug, Serialize)]
87pub struct Crate {
88 pub id: CrateNum,
89 pub name: Symbol,
90 pub is_local: bool,
91}
92
93impl Crate {
94 pub fn foreign_modules(&self) -> Vec<ForeignModuleDef> {
96 with(|cx| cx.foreign_modules(self.id))
97 }
98
99 pub fn trait_decls(&self) -> TraitDecls {
101 with(|cx| cx.trait_decls(self.id))
102 }
103
104 pub fn trait_impls(&self) -> ImplTraitDecls {
106 with(|cx| cx.trait_impls(self.id))
107 }
108
109 pub fn fn_defs(&self) -> Vec<FnDef> {
111 with(|cx| cx.crate_functions(self.id))
112 }
113
114 pub fn statics(&self) -> Vec<StaticDef> {
116 with(|cx| cx.crate_statics(self.id))
117 }
118}
119
120#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, Serialize)]
121pub enum ItemKind {
122 Fn,
123 Static,
124 Const,
125 Ctor(CtorKind),
126}
127
128#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, Serialize)]
129pub enum CtorKind {
130 Const,
131 Fn,
132}
133
134pub type Filename = String;
135
136crate_def_with_ty! {
137 #[derive(Serialize)]
139 pub CrateItem;
140}
141
142impl CrateItem {
143 pub fn expect_body(&self) -> mir::Body {
145 with(|cx| cx.mir_body(self.0))
146 }
147
148 pub fn body(&self) -> Option<mir::Body> {
150 with(|cx| cx.has_body(self.0).then(|| cx.mir_body(self.0)))
151 }
152
153 pub fn has_body(&self) -> bool {
155 with(|cx| cx.has_body(self.0))
156 }
157
158 pub fn span(&self) -> Span {
159 with(|cx| cx.span_of_an_item(self.0))
160 }
161
162 pub fn kind(&self) -> ItemKind {
163 with(|cx| cx.item_kind(*self))
164 }
165
166 pub fn requires_monomorphization(&self) -> bool {
167 with(|cx| cx.requires_monomorphization(self.0))
168 }
169
170 pub fn ty(&self) -> Ty {
171 with(|cx| cx.def_ty(self.0))
172 }
173
174 pub fn is_foreign_item(&self) -> bool {
175 with(|cx| cx.is_foreign_item(self.0))
176 }
177
178 pub fn emit_mir<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
180 self.body()
181 .ok_or_else(|| io::Error::other(format!("No body found for `{}`", self.name())))?
182 .dump(w, &self.name())
183 }
184}
185
186pub fn entry_fn() -> Option<CrateItem> {
190 with(|cx| cx.entry_fn())
191}
192
193pub fn local_crate() -> Crate {
195 with(|cx| cx.local_crate())
196}
197
198pub fn find_crates(name: &str) -> Vec<Crate> {
200 with(|cx| cx.find_crates(name))
201}
202
203pub fn external_crates() -> Vec<Crate> {
205 with(|cx| cx.external_crates())
206}
207
208pub fn all_local_items() -> CrateItems {
210 with(|cx| cx.all_local_items())
211}
212
213pub fn all_trait_decls() -> TraitDecls {
214 with(|cx| cx.all_trait_decls())
215}
216
217pub fn all_trait_impls() -> ImplTraitDecls {
218 with(|cx| cx.all_trait_impls())
219}
220
221#[derive(Clone, PartialEq, Eq, Hash, Serialize)]
223pub struct Opaque(String);
224
225impl std::fmt::Display for Opaque {
226 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
227 write!(f, "{}", self.0)
228 }
229}
230
231impl std::fmt::Debug for Opaque {
232 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
233 write!(f, "{}", self.0)
234 }
235}
236
237pub fn opaque<T: Debug>(value: &T) -> Opaque {
238 Opaque(format!("{value:?}"))
239}