circom_lsp_program_structure/program_library/
function_data.rs

1use super::ast::{FillMeta, Statement};
2use super::file_definition::FileID;
3use crate::file_definition::FileLocation;
4use std::collections::HashMap;
5
6pub type FunctionInfo = HashMap<String, FunctionData>;
7
8#[derive(Clone)]
9pub struct FunctionData {
10    name: String,
11    file_id: FileID,
12    num_of_params: usize,
13    name_of_params: Vec<String>,
14    param_location: FileLocation,
15    body: Statement,
16}
17
18impl FunctionData {
19    pub fn new(
20        name: String,
21        file_id: FileID,
22        mut body: Statement,
23        num_of_params: usize,
24        name_of_params: Vec<String>,
25        param_location: FileLocation,
26        elem_id: &mut usize,
27    ) -> FunctionData {
28        body.fill(file_id, elem_id);
29        FunctionData { name, file_id, body, name_of_params, param_location, num_of_params }
30    }
31    pub fn get_file_id(&self) -> FileID {
32        self.file_id
33    }
34    pub fn get_body(&self) -> &Statement {
35        &self.body
36    }
37    pub fn get_body_as_vec(&self) -> &Vec<Statement> {
38        match &self.body {
39            Statement::Block { stmts, .. } => stmts,
40            _ => panic!("Function body should be a block"),
41        }
42    }
43    pub fn get_mut_body(&mut self) -> &mut Statement {
44        &mut self.body
45    }
46    pub fn replace_body(&mut self, new: Statement) -> Statement {
47        std::mem::replace(&mut self.body, new)
48    }
49    pub fn get_mut_body_as_vec(&mut self) -> &mut Vec<Statement> {
50        match &mut self.body {
51            Statement::Block { stmts, .. } => stmts,
52            _ => panic!("Function body should be a block"),
53        }
54    }
55    pub fn get_param_location(&self) -> FileLocation {
56        self.param_location.clone()
57    }
58    pub fn get_num_of_params(&self) -> usize {
59        self.num_of_params
60    }
61    pub fn get_name_of_params(&self) -> &Vec<String> {
62        &self.name_of_params
63    }
64    pub fn get_name(&self) -> &str {
65        &self.name
66    }
67}