1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! Intermediate Representation (IR) module
//!
//! ## Safety Note
//! IR operations use fallible indexing with proper error handling.
//! Production code MUST NOT use unwrap() (Cloudflare-class defect prevention).
//!
//! ## Module structure
//! - `convert`: Core AST-to-IR entry point, shadow/loop helpers
//! - `convert_stmt`: Top-level statement conversion (`convert_stmt`, `convert_stmts`)
//! - `convert_fn`: Function-context statement conversion (`convert_stmt_in_function`)
//! - `expr`: Expression-to-ShellValue conversion (operators, literals, index)
//! - `expr_calls`: Function/method call expression converters
//! - `pattern`: Match/pattern and range handling
//! - `optimizations`: Constant folding, dead code elimination, IR transforms
pub mod dockerfile_ir;
pub mod effects;
pub mod shell_ir;
mod convert;
mod convert_fn;
mod convert_stmt;
mod expr;
mod expr_calls;
mod optimizations;
mod pattern;
#[cfg(test)]
mod tests;
#[cfg(test)]
#[path = "ir_expr_tests.rs"]
mod ir_expr_tests;
#[cfg(test)]
mod control_flow_tests;
#[cfg(test)]
mod convert_expr_tests;
#[cfg(test)]
mod convert_expr_tests2;
#[cfg(test)]
mod convert_expr_tests3;
#[cfg(test)]
#[path = "convert_coverage_tests.rs"]
mod convert_coverage_tests;
#[cfg(test)]
#[path = "binary_ops_coverage_tests.rs"]
mod binary_ops_coverage_tests;
#[cfg(test)]
#[path = "ir_pattern_tests.rs"]
mod ir_pattern_tests;
pub use effects::{Effect, EffectSet};
pub use shell_ir::{Command, ShellExpression, ShellIR, ShellValue};
use crate::ast::RestrictedAst;
use crate::models::{Config, Result};
/// Convert AST to Shell IR
pub fn from_ast(ast: &RestrictedAst) -> Result<ShellIR> {
let converter = IrConverter::new();
converter.convert(ast)
}
/// Optimize Shell IR based on configuration
pub fn optimize(ir: ShellIR, config: &Config) -> Result<ShellIR> {
if !config.optimize {
return Ok(ir);
}
let mut optimized = ir;
// Apply constant folding
optimized = optimizations::constant_fold(optimized);
// Apply dead code elimination
optimized = optimizations::eliminate_dead_code(optimized);
Ok(optimized)
}
struct IrConverter {
/// Track array variables: name → element count
/// Used to expand `for x in arr` into `for x in "$arr_0" "$arr_1" ...`
arrays: std::cell::RefCell<std::collections::HashMap<String, usize>>,
/// Track declared variables for shadow detection in loop bodies.
/// When a `let x = ...` (declaration=true) appears inside a loop body
/// and `x` is already in this set, it's a shadow that needs renaming.
declared_vars: std::cell::RefCell<std::collections::HashSet<String>>,
}
#[cfg(test)]
#[path = "tests_s2.rs"]
mod tests_s2;
#[cfg(test)]
#[path = "tests_s3.rs"]
mod tests_s3;
// FIXME(PMAT-238): #[cfg(test)]
#[cfg(test)]
#[path = "tests_s4.rs"]
mod tests_s4;
// FIXME(PMAT-238): #[cfg(test)]
#[cfg(test)]
#[path = "tests_s5.rs"]
mod tests_s5;
#[cfg(test)]
#[path = "tests_s6.rs"]
mod tests_s6;