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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//! Variable reference validator
//!
//! This visitor validates that all variable references in expressions
//! correspond to declared components using a SymbolTable.
use crate::ir::analysis::reference_checker::collect_imported_packages;
use crate::ir::analysis::symbol_table::SymbolTable;
use crate::ir::ast::{
ClassDefinition, ComponentReference, Equation, Expression, Statement, Variability,
};
use crate::ir::visitor::MutVisitor;
/// Visitor that validates all variable references exist
pub struct VarValidator {
/// Symbol table for tracking declared variables
symbol_table: SymbolTable,
/// Imported package root names (e.g., "Modelica" from "import Modelica;")
imported_packages: std::collections::HashSet<String>,
/// Undefined variables found
pub undefined_vars: Vec<(String, String)>, // (var_name, context)
/// Stack of loop index names for scoping (for/array comprehension)
loop_index_stack: Vec<Vec<String>>,
}
impl VarValidator {
pub fn new(class: &ClassDefinition) -> Self {
Self::with_context(class, &[], &[])
}
/// Create a validator with additional function names that should be considered valid
pub fn with_functions(class: &ClassDefinition, function_names: &[String]) -> Self {
Self::with_context(class, function_names, &[])
}
/// Create a validator with both function names and peer class names
pub fn with_context(
class: &ClassDefinition,
function_names: &[String],
peer_class_names: &[String],
) -> Self {
let mut symbol_table = SymbolTable::new();
// Add function names as global symbols
for name in function_names {
symbol_table.add_global(name);
}
// Add peer class names as global symbols (for cross-class type references)
// This allows references like `SwitchController.SwitchState` from another class
for name in peer_class_names {
symbol_table.add_global(name);
}
// Collect all declared component names
for (name, comp) in &class.components {
let is_parameter = matches!(comp.variability, Variability::Parameter(_));
symbol_table.add_symbol(name, name, &comp.type_name.to_string(), is_parameter);
}
// Add nested class names as global symbols (includes types and enumerations)
// This allows references like `State.Off` where `State` is a nested type definition
for name in class.classes.keys() {
symbol_table.add_global(name);
}
// Collect imported package root names from the class's imports
// Uses the shared collect_imported_packages from reference_checker
let imported_packages = collect_imported_packages(&class.imports);
Self {
symbol_table,
imported_packages,
undefined_vars: Vec::new(),
loop_index_stack: Vec::new(),
}
}
/// Push loop indices onto the stack (entering a for loop or array comprehension)
fn push_loop_indices(&mut self, indices: &[crate::ir::ast::ForIndex]) {
let names: Vec<String> = indices.iter().map(|i| i.ident.text.clone()).collect();
for name in &names {
self.symbol_table.add_global(name);
}
self.loop_index_stack.push(names);
}
/// Pop loop indices from the stack (exiting a for loop or array comprehension)
fn pop_loop_indices(&mut self) {
if let Some(names) = self.loop_index_stack.pop() {
for name in &names {
self.symbol_table.remove(name);
}
}
}
fn check_component_ref(&mut self, comp_ref: &ComponentReference, context: &str) {
// Build the full qualified name from all parts
let full_name = comp_ref.to_string();
// Check the first part of the reference
if let Some(first_part) = comp_ref.parts.first() {
let first_name = &first_part.ident.text;
// Skip validation if any of these are true:
// 1. The first part is in the symbol table (declared variable or built-in)
// 2. The full qualified name is in the symbol table (e.g., "D.x_start")
// 3. The first part is an imported package root (e.g., "Modelica")
// 4. There's a component that starts with this prefix (e.g., "D" when "D.x" exists)
if self.symbol_table.contains(first_name)
|| self.symbol_table.contains(&full_name)
|| self.imported_packages.contains(first_name)
|| self.symbol_table.has_prefix(first_name)
{
return;
}
self.undefined_vars
.push((first_name.clone(), context.to_string()));
}
}
}
impl MutVisitor for VarValidator {
fn enter_statement(&mut self, stmt: &mut Statement) {
if let Statement::For { indices, .. } = stmt {
self.push_loop_indices(indices);
}
}
fn exit_statement(&mut self, stmt: &mut Statement) {
if let Statement::For { .. } = stmt {
self.pop_loop_indices();
}
}
fn enter_equation(&mut self, eq: &mut Equation) {
if let Equation::For { indices, .. } = eq {
self.push_loop_indices(indices);
}
}
fn exit_equation(&mut self, eq: &mut Equation) {
if let Equation::For { .. } = eq {
self.pop_loop_indices();
}
}
fn enter_expression(&mut self, expr: &mut Expression) {
match expr {
Expression::ArrayComprehension { indices, .. } => {
self.push_loop_indices(indices);
}
Expression::ComponentReference(comp_ref) => {
self.check_component_ref(comp_ref, "expression");
}
Expression::FunctionCall { comp, .. } => {
self.check_component_ref(comp, "function call");
}
_ => {}
}
}
fn exit_expression(&mut self, expr: &mut Expression) {
if let Expression::ArrayComprehension { .. } = expr {
self.pop_loop_indices();
}
}
}