|
13 | 13 | use hir; |
14 | 14 | use hir::def_id::DefId; |
15 | 15 | use hir::map::DefPathData; |
16 | | -use mir::{Mir, Promoted}; |
| 16 | +use mir::Promoted; |
17 | 17 | use ty::TyCtxt; |
18 | | -use std::rc::Rc; |
19 | 18 | use syntax::ast::NodeId; |
20 | 19 |
|
21 | | -use std::borrow::Cow; |
22 | | - |
23 | 20 | /// Where a specific Mir comes from. |
24 | 21 | #[derive(Debug, Copy, Clone)] |
25 | 22 | pub enum MirSource { |
@@ -79,112 +76,3 @@ impl<'a, 'gcx, 'tcx> MirSource { |
79 | 76 | } |
80 | 77 | } |
81 | 78 | } |
82 | | - |
83 | | -/// Generates a default name for the pass based on the name of the |
84 | | -/// type `T`. |
85 | | -pub fn default_name<T: ?Sized>() -> Cow<'static, str> { |
86 | | - let name = unsafe { ::std::intrinsics::type_name::<T>() }; |
87 | | - if let Some(tail) = name.rfind(":") { |
88 | | - Cow::from(&name[tail+1..]) |
89 | | - } else { |
90 | | - Cow::from(name) |
91 | | - } |
92 | | -} |
93 | | - |
94 | | -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] |
95 | | -pub struct MirSuite(pub usize); |
96 | | - |
97 | | -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] |
98 | | -pub struct MirPassIndex(pub usize); |
99 | | - |
100 | | -/// A pass hook is invoked both before and after each pass executes. |
101 | | -/// This is primarily used to dump MIR for debugging. |
102 | | -/// |
103 | | -/// You can tell whether this is before or after by inspecting the |
104 | | -/// `mir` parameter -- before the pass executes, it will be `None` (in |
105 | | -/// which case you can inspect the MIR from previous pass by executing |
106 | | -/// `mir_cx.read_previous_mir()`); after the pass executes, it will be |
107 | | -/// `Some()` with the result of the pass (in which case the output |
108 | | -/// from the previous pass is most likely stolen, so you would not |
109 | | -/// want to try and access it). If the pass is interprocedural, then |
110 | | -/// the hook will be invoked once per output. |
111 | | -pub trait PassHook { |
112 | | - fn on_mir_pass<'a, 'tcx: 'a>(&self, |
113 | | - tcx: TyCtxt<'a, 'tcx, 'tcx>, |
114 | | - suite: MirSuite, |
115 | | - pass_num: MirPassIndex, |
116 | | - pass_name: &str, |
117 | | - source: MirSource, |
118 | | - mir: &Mir<'tcx>, |
119 | | - is_after: bool); |
120 | | -} |
121 | | - |
122 | | -/// The full suite of types that identifies a particular |
123 | | -/// application of a pass to a def-id. |
124 | | -pub type PassId = (MirSuite, MirPassIndex, DefId); |
125 | | - |
126 | | -/// A streamlined trait that you can implement to create a pass; the |
127 | | -/// pass will be named after the type, and it will consist of a main |
128 | | -/// loop that goes over each available MIR and applies `run_pass`. |
129 | | -pub trait MirPass { |
130 | | - fn name<'a>(&'a self) -> Cow<'a, str> { |
131 | | - default_name::<Self>() |
132 | | - } |
133 | | - |
134 | | - fn run_pass<'a, 'tcx>(&self, |
135 | | - tcx: TyCtxt<'a, 'tcx, 'tcx>, |
136 | | - source: MirSource, |
137 | | - mir: &mut Mir<'tcx>); |
138 | | -} |
139 | | - |
140 | | -/// A manager for MIR passes. |
141 | | -/// |
142 | | -/// FIXME(#41712) -- it is unclear whether we should have this struct. |
143 | | -#[derive(Clone)] |
144 | | -pub struct Passes { |
145 | | - pass_hooks: Vec<Rc<PassHook>>, |
146 | | - suites: Vec<Vec<Rc<MirPass>>>, |
147 | | -} |
148 | | - |
149 | | -/// The number of "pass suites" that we have: |
150 | | -/// |
151 | | -/// - ready for constant evaluation |
152 | | -/// - unopt |
153 | | -/// - optimized |
154 | | -pub const MIR_SUITES: usize = 3; |
155 | | - |
156 | | -/// Run the passes we need to do constant qualification and evaluation. |
157 | | -pub const MIR_CONST: MirSuite = MirSuite(0); |
158 | | - |
159 | | -/// Run the passes we need to consider the MIR validated and ready for borrowck etc. |
160 | | -pub const MIR_VALIDATED: MirSuite = MirSuite(1); |
161 | | - |
162 | | -/// Run the passes we need to consider the MIR *optimized*. |
163 | | -pub const MIR_OPTIMIZED: MirSuite = MirSuite(2); |
164 | | - |
165 | | -impl<'a, 'tcx> Passes { |
166 | | - pub fn new() -> Passes { |
167 | | - Passes { |
168 | | - pass_hooks: Vec::new(), |
169 | | - suites: (0..MIR_SUITES).map(|_| Vec::new()).collect(), |
170 | | - } |
171 | | - } |
172 | | - |
173 | | - /// Pushes a built-in pass. |
174 | | - pub fn push_pass<T: MirPass + 'static>(&mut self, suite: MirSuite, pass: T) { |
175 | | - self.suites[suite.0].push(Rc::new(pass)); |
176 | | - } |
177 | | - |
178 | | - /// Pushes a pass hook. |
179 | | - pub fn push_hook<T: PassHook + 'static>(&mut self, hook: T) { |
180 | | - self.pass_hooks.push(Rc::new(hook)); |
181 | | - } |
182 | | - |
183 | | - pub fn passes(&self, suite: MirSuite) -> &[Rc<MirPass>] { |
184 | | - &self.suites[suite.0] |
185 | | - } |
186 | | - |
187 | | - pub fn hooks(&self) -> &[Rc<PassHook>] { |
188 | | - &self.pass_hooks |
189 | | - } |
190 | | -} |
0 commit comments