Skip to main content

luaur_analysis/methods/
constraint_generator_child_scope.rs

1use crate::records::constraint_generator::ConstraintGenerator;
2use crate::records::module::Module;
3use crate::records::scope::Scope;
4use crate::type_aliases::scope_ptr_constraint_generator::ScopePtr;
5use luaur_ast::records::ast_node::AstNode;
6
7impl ConstraintGenerator {
8    pub fn child_scope(&mut self, node: *mut AstNode, parent: &ScopePtr) -> ScopePtr {
9        let scope: ScopePtr = alloc::sync::Arc::new(Scope::new(parent, 0));
10        let scope_raw = scope.as_ref() as *const Scope as *mut Scope;
11        self.scopes
12            .push((unsafe { (*node).location }, scope.clone()));
13
14        unsafe {
15            (*scope_raw).location = (*node).location;
16            (*scope_raw).return_type = parent.return_type;
17            (*scope_raw).vararg_pack = parent.vararg_pack;
18
19            let parent_raw = parent.as_ref() as *const Scope as *mut Scope;
20            (*parent_raw).children.push(scope_raw);
21        }
22
23        if let Some(module) = &self.module {
24            let module_ptr = alloc::sync::Arc::as_ptr(module) as *mut Module;
25            unsafe {
26                *(*module_ptr)
27                    .ast_scopes
28                    .get_or_insert(node as *const AstNode) = scope_raw;
29            }
30        }
31
32        scope
33    }
34}