Skip to main content

luaur_ast/methods/
parser_prepare_function_arguments.rs

1use crate::records::ast_array::AstArray;
2use crate::records::ast_local::AstLocal;
3use crate::records::binding::Binding;
4use crate::records::location::Location;
5use crate::records::parser::Parser;
6use crate::records::temp_vector::TempVector;
7
8impl Parser {
9    #[allow(non_snake_case)]
10    pub fn prepare_function_arguments(
11        &mut self,
12        start: &Location,
13        hasself: bool,
14        args: &TempVector<'_, Binding>,
15    ) -> (*mut AstLocal, AstArray<*mut AstLocal>) {
16        let mut self_local: *mut AstLocal = core::ptr::null_mut();
17
18        if hasself {
19            // C++: push_local(Binding(Name(name_self, start), nullptr));
20            // `Parser::Name { name, location }` is the (AstName, Location) pair.
21            let binding = Binding::new(
22                crate::records::name::Name {
23                    name: self.name_self,
24                    location: *start,
25                },
26                core::ptr::null_mut(),
27                crate::records::position::Position::default(),
28                false,
29            );
30            self_local = self.push_local(&binding);
31        }
32
33        // C++ uses a `TempVector<AstLocal*> vars(scratch_local)` here; a local Vec
34        // produces an identical `copy` result and avoids holding a borrow of
35        // `self.scratch_local` across the `self.push_local` calls (the scratch
36        // buffer is only an allocation-reuse optimization, not observable).
37        let mut vars: alloc::vec::Vec<*mut AstLocal> = alloc::vec::Vec::new();
38        for i in 0..args.size() {
39            let local = self.push_local(&args[i]);
40            vars.push(local);
41        }
42
43        let copied = self.copy_initializer_list_t(&vars);
44        (self_local, copied)
45    }
46}