Skip to main content

luaur_compiler/methods/
compiler_are_locals_redundant.rs

1use crate::records::compiler::Compiler;
2use crate::records::variable::Variable;
3use luaur_ast::records::ast_local::AstLocal;
4use luaur_ast::records::ast_stat_local::AstStatLocal;
5
6impl Compiler {
7    pub fn are_locals_redundant(&mut self, stat: *mut AstStatLocal) -> bool {
8        unsafe {
9            if stat.is_null() {
10                return false;
11            }
12
13            let stat_ref = &*stat;
14
15            // Extra expressions may have side effects
16            if stat_ref.values.len() > stat_ref.vars.len() {
17                return false;
18            }
19
20            for i in 0..stat_ref.vars.len() {
21                let local_ptr = *stat_ref
22                    .vars
23                    .as_slice()
24                    .get(i)
25                    .unwrap_or(&core::ptr::null_mut());
26                if local_ptr.is_null() {
27                    return false;
28                }
29
30                let local = &*local_ptr;
31
32                if local.is_exported {
33                    // exported locals must be written to the export table
34                    return false;
35                }
36
37                let v_opt = self.variables.find(&local_ptr);
38
39                let v_ptr = match v_opt {
40                    Some(ptr) => ptr,
41                    None => return false,
42                };
43
44                let v: &Variable = &*v_ptr;
45                if !v.constant {
46                    return false;
47                }
48            }
49        }
50
51        true
52    }
53}