Skip to main content

luaur_analysis/methods/
constraint_generator_prepopulate_global_scope.rs

1use crate::records::constraint_generator::ConstraintGenerator;
2use crate::records::global_prepopulator::GlobalPrepopulator;
3use crate::records::scope::Scope;
4use crate::type_aliases::scope_ptr_constraint_generator::ScopePtr;
5use core::ptr::NonNull;
6use luaur_ast::records::ast_name::AstName;
7use luaur_ast::records::ast_stat_block::AstStatBlock;
8use luaur_ast::visit::AstVisitable;
9use luaur_common::records::dense_hash_set::DenseHashSet;
10
11impl ConstraintGenerator {
12    pub fn prepopulate_global_scope(
13        &mut self,
14        global_scope: &ScopePtr,
15        program: *mut AstStatBlock,
16    ) {
17        let global_scope_raw = global_scope.as_ref() as *const Scope as *mut Scope;
18        let mut gp = GlobalPrepopulator {
19            global_scope: unsafe { NonNull::new_unchecked(global_scope_raw) },
20            arena: unsafe { NonNull::new_unchecked(self.arena) },
21            dfg: unsafe { NonNull::new_unchecked(self.dfg as *mut _) },
22            uninitialized_globals: DenseHashSet::new(AstName {
23                value: core::ptr::null(),
24            }),
25        };
26
27        if let Some(module) = &self.module {
28            (self.prepare_module_scope)(&module.name, global_scope);
29        }
30
31        unsafe {
32            (*program).visit(&mut gp);
33        }
34
35        for name in gp.uninitialized_globals.iter() {
36            self.uninitialized_globals.insert(name);
37        }
38
39        let root_scope_raw = unsafe {
40            (*self.type_function_runtime).root_scope.as_ref() as *const Scope as *mut Scope
41        };
42
43        let mut tfgp = GlobalPrepopulator {
44            global_scope: unsafe { NonNull::new_unchecked(root_scope_raw) },
45            arena: unsafe { NonNull::new_unchecked(self.arena) },
46            dfg: unsafe { NonNull::new_unchecked(self.dfg as *mut _) },
47            uninitialized_globals: DenseHashSet::new(AstName {
48                value: core::ptr::null(),
49            }),
50        };
51
52        unsafe {
53            (*program).visit(&mut tfgp);
54        }
55
56        for name in tfgp.uninitialized_globals.iter() {
57            self.uninitialized_globals.insert(name);
58        }
59    }
60}