rumoca 0.7.28

Modelica compiler written in RUST
Documentation
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! Reference checking for Modelica code.
//!
//! This module provides unified reference checking functionality used by
//! both the linter, LSP diagnostics, and the compiler's VarValidator. It checks
//! for undefined variable references while properly handling scoped constructs
//! like for-loops and array comprehensions.
//!
//! ## Configuration
//!
//! Use [`ReferenceCheckConfig`] to customize reference checking behavior:
//! - `imported_packages`: Package roots that should be considered valid (e.g., "Modelica")
//! - `additional_globals`: Extra global symbols beyond what's in the SymbolTable

use std::collections::{HashMap, HashSet};

use crate::ir::analysis::symbol_table::SymbolTable;
use crate::ir::analysis::symbols::{DefinedSymbol, add_loop_indices_to_defined};
use crate::ir::ast::{
    ClassDefinition, ComponentReference, Equation, Expression, Import, Statement, Subscript,
};

/// Configuration for reference checking.
///
/// This allows customizing how reference checking behaves, particularly for
/// handling imports and additional global symbols.
#[derive(Clone, Debug, Default)]
pub struct ReferenceCheckConfig {
    /// Imported package root names (e.g., "Modelica" from "import Modelica.Math.*;")
    /// References starting with these names are considered valid.
    pub imported_packages: HashSet<String>,
    /// Additional global symbols that should be considered valid.
    /// Useful for peer class names, external function names, etc.
    pub additional_globals: HashSet<String>,
}

impl ReferenceCheckConfig {
    /// Create a new empty configuration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a configuration from a class's imports.
    ///
    /// Extracts the root package names from all imports in the class.
    pub fn from_imports(imports: &[Import]) -> Self {
        Self {
            imported_packages: collect_imported_packages(imports),
            additional_globals: HashSet::new(),
        }
    }

    /// Add imported package roots.
    pub fn with_imported_packages(mut self, packages: HashSet<String>) -> Self {
        self.imported_packages = packages;
        self
    }

    /// Add additional global symbols.
    pub fn with_additional_globals(mut self, globals: HashSet<String>) -> Self {
        self.additional_globals = globals;
        self
    }
}

/// Collect the root package names from imports.
///
/// For example:
/// - `import Modelica;` -> "Modelica"
/// - `import Modelica.Math.*;` -> "Modelica"
/// - `import SI = Modelica.Units.SI;` -> "Modelica" (the actual package root)
pub fn collect_imported_packages(imports: &[Import]) -> HashSet<String> {
    let mut packages = HashSet::new();

    for import in imports {
        match import {
            Import::Qualified { path, .. } => {
                // import A.B.C; -> root is "A"
                if let Some(first) = path.name.first() {
                    packages.insert(first.text.clone());
                }
            }
            Import::Renamed { path, .. } => {
                // import D = A.B.C; -> root is "A"
                if let Some(first) = path.name.first() {
                    packages.insert(first.text.clone());
                }
            }
            Import::Unqualified { path, .. } => {
                // import A.B.*; -> root is "A"
                if let Some(first) = path.name.first() {
                    packages.insert(first.text.clone());
                }
            }
            Import::Selective { path, .. } => {
                // import A.B.{C, D}; -> root is "A"
                if let Some(first) = path.name.first() {
                    packages.insert(first.text.clone());
                }
            }
        }
    }

    packages
}

/// A reference error found during checking.
#[derive(Clone, Debug)]
pub struct ReferenceError {
    /// The undefined variable name
    pub name: String,
    /// Source line number (1-based)
    pub line: u32,
    /// Source column number (1-based)
    pub col: u32,
    /// Error message
    pub message: String,
}

impl ReferenceError {
    fn undefined_variable(name: &str, line: u32, col: u32) -> Self {
        Self {
            name: name.to_string(),
            line,
            col,
            message: format!("Undefined variable '{}'", name),
        }
    }
}

/// Result of reference checking.
#[derive(Clone, Debug, Default)]
pub struct ReferenceCheckResult {
    /// Reference errors found
    pub errors: Vec<ReferenceError>,
    /// All symbols that were used/referenced
    pub used_symbols: HashSet<String>,
}

/// Check a class for undefined references.
///
/// This function checks all equations, statements, and component start
/// expressions for references to undefined variables.
///
/// # Arguments
/// * `class` - The class definition to check
/// * `defined` - Map of locally defined symbols
/// * `scope` - Symbol table for global/parent scope resolution
///
/// # Returns
/// A `ReferenceCheckResult` containing any errors and the set of used symbols.
pub fn check_class_references(
    class: &ClassDefinition,
    defined: &HashMap<String, DefinedSymbol>,
    scope: &SymbolTable,
) -> ReferenceCheckResult {
    check_class_references_with_config(class, defined, scope, &ReferenceCheckConfig::default())
}

/// Check a class for undefined references with custom configuration.
///
/// This function checks all equations, statements, and component start
/// expressions for references to undefined variables, using the provided
/// configuration for import handling and additional globals.
///
/// # Arguments
/// * `class` - The class definition to check
/// * `defined` - Map of locally defined symbols
/// * `scope` - Symbol table for global/parent scope resolution
/// * `config` - Configuration for reference checking behavior
///
/// # Returns
/// A `ReferenceCheckResult` containing any errors and the set of used symbols.
pub fn check_class_references_with_config(
    class: &ClassDefinition,
    defined: &HashMap<String, DefinedSymbol>,
    scope: &SymbolTable,
    config: &ReferenceCheckConfig,
) -> ReferenceCheckResult {
    let mut result = ReferenceCheckResult::default();

    // Check all equations (regular + initial)
    for eq in class.iter_all_equations() {
        check_equation(eq, defined, scope, config, &mut result);
    }

    // Check all statements (algorithms + initial algorithms)
    for stmt in class.iter_all_statements() {
        check_statement(stmt, defined, scope, config, &mut result);
    }

    // Check component start expressions
    for (_, comp) in class.iter_components() {
        check_expression(&comp.start, defined, scope, config, &mut result);
    }

    result
}

fn check_equation(
    eq: &Equation,
    defined: &HashMap<String, DefinedSymbol>,
    scope: &SymbolTable,
    config: &ReferenceCheckConfig,
    result: &mut ReferenceCheckResult,
) {
    match eq {
        Equation::Empty => {}
        Equation::Simple { lhs, rhs } => {
            check_expression(lhs, defined, scope, config, result);
            check_expression(rhs, defined, scope, config, result);
        }
        Equation::Connect { lhs, rhs } => {
            check_component_ref(lhs, defined, scope, config, result);
            check_component_ref(rhs, defined, scope, config, result);
        }
        Equation::For { indices, equations } => {
            // Add loop indices as locally defined
            let mut local_defined = defined.clone();
            add_loop_indices_to_defined(indices, &mut local_defined);

            // Check range expressions
            for index in indices {
                check_expression(&index.range, &local_defined, scope, config, result);
            }

            // Check nested equations with extended scope
            for sub_eq in equations {
                check_equation(sub_eq, &local_defined, scope, config, result);
            }
        }
        Equation::When(blocks) => {
            for block in blocks {
                check_expression(&block.cond, defined, scope, config, result);
                for sub_eq in &block.eqs {
                    check_equation(sub_eq, defined, scope, config, result);
                }
            }
        }
        Equation::If {
            cond_blocks,
            else_block,
        } => {
            for block in cond_blocks {
                check_expression(&block.cond, defined, scope, config, result);
                for sub_eq in &block.eqs {
                    check_equation(sub_eq, defined, scope, config, result);
                }
            }
            if let Some(else_eqs) = else_block {
                for sub_eq in else_eqs {
                    check_equation(sub_eq, defined, scope, config, result);
                }
            }
        }
        Equation::FunctionCall { comp: _, args } => {
            // Don't check function name - it might be external
            for arg in args {
                check_expression(arg, defined, scope, config, result);
            }
        }
    }
}

fn check_statement(
    stmt: &Statement,
    defined: &HashMap<String, DefinedSymbol>,
    scope: &SymbolTable,
    config: &ReferenceCheckConfig,
    result: &mut ReferenceCheckResult,
) {
    match stmt {
        Statement::Empty => {}
        Statement::Assignment { comp, value } => {
            check_component_ref(comp, defined, scope, config, result);
            check_expression(value, defined, scope, config, result);
        }
        Statement::FunctionCall {
            comp: _,
            args,
            outputs,
        } => {
            // Don't check function name - it might be external
            for arg in args {
                check_expression(arg, defined, scope, config, result);
            }
            // Check output expressions (the variables being assigned to)
            for output in outputs {
                check_expression(output, defined, scope, config, result);
            }
        }
        Statement::For { indices, equations } => {
            // Add loop indices as locally defined
            let mut local_defined = defined.clone();
            add_loop_indices_to_defined(indices, &mut local_defined);

            // Check range expressions
            for index in indices {
                check_expression(&index.range, &local_defined, scope, config, result);
            }

            // Check nested statements with extended scope
            for sub_stmt in equations {
                check_statement(sub_stmt, &local_defined, scope, config, result);
            }
        }
        Statement::While(block) => {
            check_expression(&block.cond, defined, scope, config, result);
            for sub_stmt in &block.stmts {
                check_statement(sub_stmt, defined, scope, config, result);
            }
        }
        Statement::If {
            cond_blocks,
            else_block,
        } => {
            for block in cond_blocks {
                check_expression(&block.cond, defined, scope, config, result);
                for sub_stmt in &block.stmts {
                    check_statement(sub_stmt, defined, scope, config, result);
                }
            }
            if let Some(else_stmts) = else_block {
                for sub_stmt in else_stmts {
                    check_statement(sub_stmt, defined, scope, config, result);
                }
            }
        }
        Statement::When(blocks) => {
            for block in blocks {
                check_expression(&block.cond, defined, scope, config, result);
                for sub_stmt in &block.stmts {
                    check_statement(sub_stmt, defined, scope, config, result);
                }
            }
        }
        Statement::Return { .. } | Statement::Break { .. } => {}
    }
}

fn check_expression(
    expr: &Expression,
    defined: &HashMap<String, DefinedSymbol>,
    scope: &SymbolTable,
    config: &ReferenceCheckConfig,
    result: &mut ReferenceCheckResult,
) {
    match expr {
        Expression::Empty => {}
        Expression::ComponentReference(comp_ref) => {
            check_component_ref(comp_ref, defined, scope, config, result);
        }
        Expression::Terminal { .. } => {}
        Expression::FunctionCall { comp, args } => {
            // Function name might be external, but check subscripts
            for part in &comp.parts {
                if let Some(subs) = &part.subs {
                    for sub in subs {
                        if let Subscript::Expression(sub_expr) = sub {
                            check_expression(sub_expr, defined, scope, config, result);
                        }
                    }
                }
            }
            for arg in args {
                check_expression(arg, defined, scope, config, result);
            }
        }
        Expression::Binary { lhs, rhs, .. } => {
            check_expression(lhs, defined, scope, config, result);
            check_expression(rhs, defined, scope, config, result);
        }
        Expression::Unary { rhs, .. } => {
            check_expression(rhs, defined, scope, config, result);
        }
        Expression::Array { elements, .. } => {
            for elem in elements {
                check_expression(elem, defined, scope, config, result);
            }
        }
        Expression::Tuple { elements } => {
            for elem in elements {
                check_expression(elem, defined, scope, config, result);
            }
        }
        Expression::If {
            branches,
            else_branch,
        } => {
            for (cond, then_expr) in branches {
                check_expression(cond, defined, scope, config, result);
                check_expression(then_expr, defined, scope, config, result);
            }
            check_expression(else_branch, defined, scope, config, result);
        }
        Expression::Range { start, step, end } => {
            check_expression(start, defined, scope, config, result);
            if let Some(s) = step {
                check_expression(s, defined, scope, config, result);
            }
            check_expression(end, defined, scope, config, result);
        }
        Expression::Parenthesized { inner } => {
            check_expression(inner, defined, scope, config, result);
        }
        Expression::ArrayComprehension { expr, indices } => {
            // Array comprehension indices are locally defined
            let mut local_defined = defined.clone();
            add_loop_indices_to_defined(indices, &mut local_defined);

            check_expression(expr, &local_defined, scope, config, result);
            for idx in indices {
                check_expression(&idx.range, &local_defined, scope, config, result);
            }
        }
    }
}

fn check_component_ref(
    comp_ref: &ComponentReference,
    defined: &HashMap<String, DefinedSymbol>,
    scope: &SymbolTable,
    config: &ReferenceCheckConfig,
    result: &mut ReferenceCheckResult,
) {
    if let Some(first) = comp_ref.parts.first() {
        let name = &first.ident.text;

        // Track as used
        result.used_symbols.insert(name.clone());

        // Check if defined locally, globally, in imported packages, or in additional globals
        let is_defined = defined.contains_key(name)
            || scope.contains(name)
            || config.imported_packages.contains(name)
            || config.additional_globals.contains(name);

        if !is_defined {
            result.errors.push(ReferenceError::undefined_variable(
                name,
                first.ident.location.start_line,
                first.ident.location.start_column,
            ));
        }

        // Check subscripts
        if let Some(subs) = &first.subs {
            for sub in subs {
                if let Subscript::Expression(sub_expr) = sub {
                    check_expression(sub_expr, defined, scope, config, result);
                }
            }
        }
    }

    // Check remaining parts' subscripts
    for part in comp_ref.parts.iter().skip(1) {
        if let Some(subs) = &part.subs {
            for sub in subs {
                if let Subscript::Expression(sub_expr) = sub {
                    check_expression(sub_expr, defined, scope, config, result);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::analysis::symbols::collect_defined_symbols;
    use crate::modelica_grammar::ModelicaGrammar;
    use crate::modelica_parser::parse;

    fn parse_test_code(code: &str) -> crate::ir::ast::StoredDefinition {
        let mut grammar = ModelicaGrammar::new();
        parse(code, "test.mo", &mut grammar).expect("Failed to parse test code");
        grammar.modelica.expect("No AST produced")
    }

    #[test]
    fn test_undefined_reference() {
        let code = r#"
model Test
  Real x;
equation
  x = y + 1.0;
end Test;
"#;
        let ast = parse_test_code(code);
        let class = ast.class_list.get("Test").expect("Test class not found");

        let defined = collect_defined_symbols(class);
        let scope = SymbolTable::new();

        let result = check_class_references(class, &defined, &scope);

        assert_eq!(result.errors.len(), 1);
        assert_eq!(result.errors[0].name, "y");
    }

    #[test]
    fn test_for_loop_index() {
        let code = r#"
model Test
  Real x[10];
equation
  for i in 1:10 loop
    x[i] = i * 2.0;
  end for;
end Test;
"#;
        let ast = parse_test_code(code);
        let class = ast.class_list.get("Test").expect("Test class not found");

        let defined = collect_defined_symbols(class);
        let scope = SymbolTable::new();

        let result = check_class_references(class, &defined, &scope);

        // No errors - 'i' should be recognized as a loop index
        assert!(
            result.errors.is_empty(),
            "Expected no errors, got: {:?}",
            result.errors
        );
    }

    #[test]
    fn test_used_symbols_tracking() {
        let code = r#"
model Test
  Real x;
  Real y;
equation
  x = y + 1.0;
end Test;
"#;
        let ast = parse_test_code(code);
        let class = ast.class_list.get("Test").expect("Test class not found");

        let defined = collect_defined_symbols(class);
        let scope = SymbolTable::new();

        let result = check_class_references(class, &defined, &scope);

        assert!(result.used_symbols.contains("x"));
        assert!(result.used_symbols.contains("y"));
    }

    #[test]
    fn test_array_comprehension_index() {
        let code = r#"
model Test
  Real x[10] = {i * 2 for i in 1:10};
end Test;
"#;
        let ast = parse_test_code(code);
        let class = ast.class_list.get("Test").expect("Test class not found");

        let defined = collect_defined_symbols(class);
        let scope = SymbolTable::new();

        let result = check_class_references(class, &defined, &scope);

        // No errors - 'i' should be recognized as a comprehension index
        assert!(
            result.errors.is_empty(),
            "Expected no errors, got: {:?}",
            result.errors
        );
    }

    #[test]
    fn test_imported_package_reference() {
        let code = r#"
model Test
  Real x;
equation
  x = Modelica.Constants.pi;
end Test;
"#;
        let ast = parse_test_code(code);
        let class = ast.class_list.get("Test").expect("Test class not found");

        let defined = collect_defined_symbols(class);
        let scope = SymbolTable::new();

        // Without config, "Modelica" should be undefined
        let result = check_class_references(class, &defined, &scope);
        assert_eq!(result.errors.len(), 1);
        assert_eq!(result.errors[0].name, "Modelica");

        // With config including "Modelica" as imported package, no error
        let config = ReferenceCheckConfig::new()
            .with_imported_packages(["Modelica".to_string()].into_iter().collect());
        let result = check_class_references_with_config(class, &defined, &scope, &config);
        assert!(
            result.errors.is_empty(),
            "Expected no errors with imported package, got: {:?}",
            result.errors
        );
    }

    #[test]
    fn test_additional_globals_reference() {
        // Test with a variable reference (function calls don't check function names)
        let code = r#"
model Test
  Real x;
equation
  x = PeerClass.value;
end Test;
"#;
        let ast = parse_test_code(code);
        let class = ast.class_list.get("Test").expect("Test class not found");
        let defined = collect_defined_symbols(class);
        let scope = SymbolTable::new();

        // Without config, "PeerClass" should be undefined
        let result = check_class_references(class, &defined, &scope);
        assert_eq!(result.errors.len(), 1);
        assert_eq!(result.errors[0].name, "PeerClass");

        // With config including "PeerClass" as additional global, no error
        let config = ReferenceCheckConfig::new()
            .with_additional_globals(["PeerClass".to_string()].into_iter().collect());
        let result = check_class_references_with_config(class, &defined, &scope, &config);
        assert!(
            result.errors.is_empty(),
            "Expected no errors with additional global, got: {:?}",
            result.errors
        );
    }
}