js2zig_core/zigir/passes/
mod.rs1mod constant_fold;
8mod dead_code;
9mod validate;
10
11pub use constant_fold::ConstantFoldPass;
12pub use dead_code::DeadCodeElimPass;
13pub use validate::ValidatePass;
14
15use crate::zigir::source_span::IrDiagnostic;
16use crate::zigir::types::IrModule;
17
18#[derive(Debug, Clone)]
24pub struct PassResult {
25 pub changed: bool,
27 pub diagnostics: Vec<IrDiagnostic>,
29}
30
31impl PassResult {
32 pub fn unchanged() -> Self {
34 Self {
35 changed: false,
36 diagnostics: Vec::new(),
37 }
38 }
39
40 pub fn changed() -> Self {
42 Self {
43 changed: true,
44 diagnostics: Vec::new(),
45 }
46 }
47
48 pub fn with_diagnostics(diagnostics: Vec<IrDiagnostic>) -> Self {
52 let changed = diagnostics
53 .iter()
54 .any(|d| matches!(d.level, crate::zigir::source_span::DiagnosticLevel::Error));
55 Self {
56 changed,
57 diagnostics,
58 }
59 }
60
61 pub fn merge(&mut self, other: PassResult) {
63 self.changed = self.changed || other.changed;
64 self.diagnostics.extend(other.diagnostics);
65 }
66}
67
68pub trait IrPass {
74 fn name(&self) -> &'static str;
76
77 fn description(&self) -> &'static str;
79
80 fn run(&mut self, module: &mut IrModule) -> PassResult;
85}
86
87pub struct PassPipeline {
96 passes: Vec<Box<dyn IrPass>>,
97 pub continue_on_error: bool,
99}
100
101#[derive(Debug, Clone)]
103pub struct PipelineResult {
104 pub passes_run: usize,
107 pub changed: bool,
109 pub diagnostics: Vec<IrDiagnostic>,
111 pub changed_passes: Vec<&'static str>,
113}
114
115impl PassPipeline {
116 pub fn new() -> Self {
118 Self {
119 passes: Vec::new(),
120 continue_on_error: false,
121 }
122 }
123
124 pub fn default_pipeline() -> Self {
129 let mut pipeline = Self::new();
130 pipeline.add_pass(ValidatePass::new());
131 pipeline.add_pass(DeadCodeElimPass::new());
132 pipeline.add_pass(ConstantFoldPass::new());
133 pipeline.add_pass(ValidatePass::new());
134 pipeline
135 }
136
137 pub fn add_pass(&mut self, pass: impl IrPass + 'static) {
139 self.passes.push(Box::new(pass));
140 }
141
142 pub fn run(&mut self, module: &mut IrModule) -> PipelineResult {
144 let mut result = PipelineResult {
145 passes_run: 0,
146 changed: false,
147 diagnostics: Vec::new(),
148 changed_passes: Vec::new(),
149 };
150
151 for pass in &mut self.passes {
152 let pass_result = pass.run(module);
153 result.passes_run += 1;
154
155 let had_errors = pass_result
156 .diagnostics
157 .iter()
158 .any(|d| matches!(d.level, crate::zigir::source_span::DiagnosticLevel::Error));
159
160 result.diagnostics.extend(pass_result.diagnostics);
161
162 if pass_result.changed {
163 result.changed = true;
164 result.changed_passes.push(pass.name());
165 }
166
167 if had_errors && !self.continue_on_error {
168 break;
169 }
170 }
171
172 result
173 }
174}
175
176impl Default for PassPipeline {
177 fn default() -> Self {
178 Self::new()
179 }
180}
181
182#[cfg(test)]
187mod tests {
188 use super::*;
189 use crate::types::ZigType;
190 use crate::zigir::ident::IrIdent;
191 use crate::zigir::types::{IrDecl, IrVarDecl};
192
193 struct NoOpPass;
195
196 impl IrPass for NoOpPass {
197 fn name(&self) -> &'static str {
198 "no-op"
199 }
200 fn description(&self) -> &'static str {
201 "does nothing"
202 }
203 fn run(&mut self, _module: &mut IrModule) -> PassResult {
204 PassResult::unchanged()
205 }
206 }
207
208 struct WarnPass;
210
211 impl IrPass for WarnPass {
212 fn name(&self) -> &'static str {
213 "warn"
214 }
215 fn description(&self) -> &'static str {
216 "emits a warning"
217 }
218 fn run(&mut self, _module: &mut IrModule) -> PassResult {
219 PassResult::with_diagnostics(vec![IrDiagnostic::warning("test warning".to_string())])
220 }
221 }
222
223 struct AddVarPass;
225
226 impl IrPass for AddVarPass {
227 fn name(&self) -> &'static str {
228 "add-var"
229 }
230 fn description(&self) -> &'static str {
231 "adds a variable"
232 }
233 fn run(&mut self, module: &mut IrModule) -> PassResult {
234 module.declarations.push(IrDecl::Var(IrVarDecl {
235 name: IrIdent::new("__test_added"),
236 is_const: true,
237 zig_type: Some(ZigType::I64),
238 init: Some(crate::zigir::types::IrExpr::IntLiteral(0)),
239 is_json_parse: false,
240 needs_var_suppression: false,
241 }));
242 PassResult::changed()
243 }
244 }
245
246 #[test]
247 fn test_empty_pipeline() {
248 let mut pipeline = PassPipeline::new();
249 let mut module = IrModule::new("test".to_string());
250 let result = pipeline.run(&mut module);
251 assert_eq!(result.passes_run, 0);
252 assert!(!result.changed);
253 }
254
255 #[test]
256 fn test_noop_pass() {
257 let mut pipeline = PassPipeline::new();
258 pipeline.add_pass(NoOpPass);
259 let mut module = IrModule::new("test".to_string());
260 let result = pipeline.run(&mut module);
261 assert_eq!(result.passes_run, 1);
262 assert!(!result.changed);
263 assert!(result.changed_passes.is_empty());
264 }
265
266 #[test]
267 fn test_warn_pass() {
268 let mut pipeline = PassPipeline::new();
269 pipeline.add_pass(WarnPass);
270 let mut module = IrModule::new("test".to_string());
271 let result = pipeline.run(&mut module);
272 assert_eq!(result.passes_run, 1);
273 assert!(!result.changed); assert_eq!(result.diagnostics.len(), 1);
275 }
276
277 #[test]
278 fn test_change_detection() {
279 let mut pipeline = PassPipeline::new();
280 pipeline.add_pass(AddVarPass);
281 let mut module = IrModule::new("test".to_string());
282 let result = pipeline.run(&mut module);
283 assert_eq!(result.passes_run, 1);
284 assert!(result.changed);
285 assert_eq!(result.changed_passes, vec!["add-var"]);
286 assert_eq!(module.declarations.len(), 1);
287 }
288
289 #[test]
290 fn test_multi_pass_pipeline() {
291 let mut pipeline = PassPipeline::new();
292 pipeline.add_pass(NoOpPass);
293 pipeline.add_pass(WarnPass);
294 pipeline.add_pass(AddVarPass);
295 let mut module = IrModule::new("test".to_string());
296 let result = pipeline.run(&mut module);
297 assert_eq!(result.passes_run, 3);
298 assert!(result.changed);
299 assert_eq!(result.changed_passes, vec!["add-var"]);
300 assert_eq!(result.diagnostics.len(), 1);
301 }
302
303 #[test]
304 fn test_default_pipeline_runs() {
305 let mut pipeline = PassPipeline::default_pipeline();
306 let mut module = IrModule::new("test".to_string());
307 let result = pipeline.run(&mut module);
308 assert_eq!(result.passes_run, 4);
310 }
311
312 #[test]
313 fn test_pass_result_merge() {
314 let mut r1 = PassResult::changed();
315 let r2 = PassResult::with_diagnostics(vec![IrDiagnostic::warning("w".to_string())]);
316 r1.merge(r2);
317 assert!(r1.changed);
318 assert_eq!(r1.diagnostics.len(), 1);
319 }
320}