swamp-analyzer 0.2.21

analyzer for swamp
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
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
use crate::Analyzer;
use source_map_node::Node;
use std::rc::Rc;
use swamp_semantic::err::ErrorKind;
use swamp_semantic::ScopeInfo;
use swamp_semantic::{
    ArgumentExpression, BlockScopeMode, Expression, ExpressionKind, Variable, VariableRef,
    VariableType,
};
use swamp_symbol::{ScopedSymbolId, Symbol, SymbolKind};
use swamp_types::prelude::*;

pub const MAX_VIRTUAL_REGISTER: usize = 48;

/// Common helper function for allocating the next available register from `ScopeInfo`
/// This function uses high watermark approach - simply increment counter, restore on scope pop
pub(crate) const fn allocate_next_register(scope: &mut ScopeInfo) -> Option<u8> {
    if scope.total_scopes.current_register >= MAX_VIRTUAL_REGISTER {
        None
    } else {
        scope.total_scopes.current_register += 1;
        Some(scope.total_scopes.current_register as u8)
    }
}

impl Analyzer<'_> {
    fn try_find_local_variable(&self, node: &Node) -> Option<&VariableRef> {
        let current_scope = self
            .scope
            .active_scope
            .block_scope_stack
            .iter()
            .last()
            .expect("no scope stack available");

        let variable_text = self.get_text_resolved(node).to_string();

        current_scope.lookup.get(&variable_text)
    }

    #[allow(unused)]
    pub(crate) fn find_variable(&mut self, variable: &swamp_ast::Variable) -> VariableRef {
        if let Some(x) = self.try_find_variable(&variable.name) {
            x
        } else {
            self.add_err(ErrorKind::UnknownVariable, &variable.name);
            VariableRef::from(Variable::create_err(self.types().unit()))
        }
    }

    pub(crate) fn try_find_variable(&self, node: &swamp_ast::Node) -> Option<VariableRef> {
        let variable_text = self.get_text(node);

        for scope in self.scope.active_scope.block_scope_stack.iter().rev() {
            if let Some(value) = scope.lookup.get(&variable_text.to_string()) {
                return Some(value.clone());
            }
            if scope.mode == BlockScopeMode::Closed {
                break;
            }
        }

        None
    }

    pub(crate) fn create_parameter_resolved(
        &mut self,
        variable: &Node,
        is_mutable: Option<&Node>,
        variable_type_ref: &TypeRef,
    ) {
        let (variable_ref, _name_str) = self.create_variable_like_resolved(
            variable,
            is_mutable,
            variable_type_ref,
            VariableType::Parameter,
        );
    }

    pub(crate) fn create_local_variable(
        &mut self,
        variable: &swamp_ast::Node,
        is_mutable: Option<&swamp_ast::Node>,
        variable_type_ref: &TypeRef,
        concrete_check: bool,
    ) -> VariableRef {
        let debug_text = self.get_text(variable);
        if !debug_text.starts_with('_')
            && concrete_check
            && !variable_type_ref.can_be_stored_in_variable()
        {
            self.add_err(
                ErrorKind::VariableTypeMustBeBlittable(variable_type_ref.clone()),
                variable,
            );
            return Rc::new(Variable::create_err(self.types().unit()));
        }
        self.create_local_variable_resolved(
            &self.to_node(variable),
            Option::from(&self.to_node_option(is_mutable)),
            variable_type_ref,
        )
    }

    pub(crate) fn create_local_variable_parameter_like(
        &mut self,
        variable: &swamp_ast::Node,
        is_mutable: Option<&swamp_ast::Node>,
        variable_type_ref: &TypeRef,
        concrete_check: bool,
    ) -> VariableRef {
        let debug_text = self.get_text(variable);
        if !debug_text.starts_with('_')
            && concrete_check
            && !variable_type_ref.can_be_stored_in_variable()
        {
            self.add_err(
                ErrorKind::VariableTypeMustBeBlittable(variable_type_ref.clone()),
                variable,
            );
            return Rc::new(Variable::create_err(self.types().unit()));
        }
        self.create_local_variable_parameter_like_resolved(
            &self.to_node(variable),
            Option::from(&self.to_node_option(is_mutable)),
            variable_type_ref,
        )
    }

    pub(crate) fn create_variable(
        &mut self,
        variable: &swamp_ast::Variable,
        variable_type_ref: &TypeRef,
    ) -> VariableRef {
        self.create_local_variable(
            &variable.name,
            Option::from(&variable.is_mutable),
            variable_type_ref,
            true,
        )
    }

    pub(crate) fn create_variable_param_like(
        &mut self,
        variable: &swamp_ast::Variable,
        variable_type_ref: &TypeRef,
    ) -> VariableRef {
        self.create_local_variable_parameter_like(
            &variable.name,
            Option::from(&variable.is_mutable),
            variable_type_ref,
            true,
        )
    }

    pub(crate) fn create_local_variable_resolved(
        &mut self,
        variable: &Node,
        is_mutable: Option<&Node>,
        variable_type_ref: &TypeRef,
    ) -> VariableRef {
        let (variable_ref, variable_str) = self.create_variable_like_resolved(
            variable,
            is_mutable,
            variable_type_ref,
            VariableType::Local,
        );

        variable_ref
    }

    pub(crate) fn create_local_variable_parameter_like_resolved(
        &mut self,
        variable: &Node,
        is_mutable: Option<&Node>,
        variable_type_ref: &TypeRef,
    ) -> VariableRef {
        let (variable_ref, variable_str) = self.create_variable_like_resolved(
            variable,
            is_mutable,
            variable_type_ref,
            VariableType::Parameter,
        );

        variable_ref
    }

    pub(crate) fn create_variable_like_resolved(
        &mut self,
        variable: &Node,
        is_mutable: Option<&Node>,
        variable_type_ref: &TypeRef,
        variable_type: VariableType,
    ) -> (VariableRef, String) {
        if let Some(_existing_variable) = self.try_find_local_variable(variable) {
            self.add_err_resolved(ErrorKind::OverwriteVariableNotAllowedHere, variable);


            let error_var_ref = VariableRef::new(Variable {
                symbol_id: ScopedSymbolId::new_illegal(),
                name: Default::default(),
                assigned_name: "err".to_string(),
                resolved_type: self.types().unit(),
                mutable_node: None,
                variable_type,
                scope_index: 0,
                variable_index: 0,
                unique_id_within_function: 0,
                virtual_register: 0,
                is_unused: false,
            });

            return (error_var_ref, "err".to_string());
        }
        let variable_str = self.get_text_resolved(variable).to_string();

        let scope_index = self.scope.active_scope.block_scope_stack.len() - 1;

        let index = { self.scope.active_scope.emit_variable_index() };

        let should_insert_in_scope = !variable_str.starts_with('_');

        let variables_len = &mut self
            .scope
            .active_scope
            .block_scope_stack
            .last_mut()
            .expect("block scope should have at least one scope")
            .variables
            .len();

        // Check for unused mutable variables before incrementing register counter
        if !should_insert_in_scope && is_mutable.is_some() {
            self.add_err_resolved(ErrorKind::UnusedVariablesCanNotBeMut, variable);


            let error_var_ref = VariableRef::new(Variable {
                symbol_id: ScopedSymbolId::new_illegal(),
                name: Default::default(),
                assigned_name: "err".to_string(),
                resolved_type: self.types().unit(),
                mutable_node: None,
                variable_type,
                scope_index: 0,
                variable_index: 0,
                unique_id_within_function: 0,
                virtual_register: 0,
                is_unused: false,
            });

            return (error_var_ref, "err".to_string());
        }

        // Only increment register counter when we're actually creating a valid variable
        let maybe_virtual_register = allocate_next_register(&mut self.scope);
        if let Some(virtual_register) = maybe_virtual_register {
            let symbol_id = self.shared.state.symbol_id_allocator.alloc_scoped();
            self.shared.state.symbols.insert_scoped(symbol_id, Symbol {
                id: symbol_id.into(),
                kind: SymbolKind::Variable,
                source_map_node: variable.clone(),
                name: variable.clone(),
            });
            let resolved_variable = Variable {
                symbol_id,
                name: variable.clone(),
                assigned_name: variable_str.clone(),
                variable_type,
                resolved_type: variable_type_ref.clone(),
                mutable_node: is_mutable.cloned(),
                scope_index,
                variable_index: *variables_len,
                unique_id_within_function: index,
                virtual_register,
                is_unused: !should_insert_in_scope,
            };

            let variable_ref = Rc::new(resolved_variable);

            if should_insert_in_scope {
                let lookups = &mut self
                    .scope
                    .active_scope
                    .block_scope_stack
                    .last_mut()
                    .expect("block scope should have at least one scope")
                    .lookup;
                lookups
                    .insert(variable_str.clone(), variable_ref.clone())
                    .expect("should have checked earlier for variable");
            }

            let variables = &mut self
                .scope
                .active_scope
                .block_scope_stack
                .last_mut()
                .expect("block scope should have at least one scope")
                .variables;
            variables
                .insert(variable_ref.unique_id_within_function, variable_ref.clone())
                .expect("should have checked earlier for variable");

            self.scope
                .total_scopes
                .all_variables
                .push(variable_ref.clone());

            (variable_ref, variable_str)
        } else {
            eprintln!("variable: {variable_str}");
            for var in &self.scope.total_scopes.all_variables {
                eprintln!("var id:{} '{}'", var.virtual_register, var.assigned_name);
            }
            self.add_err_resolved(ErrorKind::OutOfVirtualRegisters, variable);
            let resolved_variable = Variable {
                symbol_id: ScopedSymbolId::new_illegal(),
                name: variable.clone(),
                assigned_name: variable_str,
                variable_type,
                resolved_type: variable_type_ref.clone(),
                mutable_node: is_mutable.cloned(),
                scope_index,
                variable_index: *variables_len,
                unique_id_within_function: index,
                virtual_register: 0,
                is_unused: true,
            };

            let variable_ref = Rc::new(resolved_variable);
            (variable_ref, "error".to_string())
        }
    }

    #[allow(clippy::too_many_lines)]
    pub(crate) fn create_variable_binding_for_with(
        &mut self,
        ast_variable: &swamp_ast::Variable,
        converted_expression: ArgumentExpression,
    ) -> Expression {
        let expression_type = converted_expression.ty();

        match converted_expression {
            ArgumentExpression::Expression(expr) | ArgumentExpression::MaterializedExpression(expr) => {
                // For immutable bindings or expressions that can't be aliased,
                // create a regular variable definition
                let variable_ref = self.create_local_variable(
                    &ast_variable.name,
                    ast_variable.is_mutable.as_ref(),
                    &expression_type,
                    false,
                );

                let var_def_kind = ExpressionKind::VariableDefinition(variable_ref, Box::new(expr));
                let unit_type = self.types().unit();
                self.create_expr(var_def_kind, unit_type, &ast_variable.name)
            }
            ArgumentExpression::BorrowMutableReference(loc) => {
                // For mutable reference bindings, check if it's a scalar or aggregate type
                if ast_variable.is_mutable.is_none() {
                    return self.create_err(ErrorKind::VariableIsNotMutable, &ast_variable.name);
                }

                // Check if the type is a scalar/primitive type
                let is_scalar = match &*expression_type.kind {
                    TypeKind::Int | TypeKind::Float | TypeKind::Bool | TypeKind::String { .. } => {
                        true
                    }
                    _ => false,
                };

                if is_scalar {
                    // For scalars, treat mutable references as copies for now
                    // TODO: Implement copy-back mechanism later
                    let original_expr = ExpressionKind::VariableAccess(loc.starting_variable);
                    let original_expr_wrapped = self.create_expr(
                        original_expr,
                        expression_type.clone(),
                        &ast_variable.name,
                    );

                    // Create a regular variable definition (copy)
                    let variable_ref = self.create_local_variable(
                        &ast_variable.name,
                        ast_variable.is_mutable.as_ref(),
                        &expression_type,
                        false,
                    );

                    let var_def_kind = ExpressionKind::VariableDefinition(
                        variable_ref,
                        Box::new(original_expr_wrapped),
                    );
                    let unit_type = self.types().unit();
                    self.create_expr(var_def_kind, unit_type, &ast_variable.name)
                } else {
                    // For aggregate types, create a proper alias using VariableDefinitionLValue
                    let variable_ref = self.create_local_variable(
                        &ast_variable.name,
                        ast_variable.is_mutable.as_ref(),
                        &expression_type,
                        false,
                    );

                    // Use the VariableDefinitionLValue expression to bind the variable to the lvalue
                    let expr_kind = ExpressionKind::VariableDefinitionLValue(variable_ref, loc);
                    let unit_type = self.types().unit();
                    self.create_expr(expr_kind, unit_type, &ast_variable.name)
                }
            }
        }
    }

    pub const fn types(&mut self) -> &mut TypeCache {
        &mut self.shared.state.types
    }
}