circom_lsp_program_structure/program_library/
program_archive.rs1use super::ast::{Definition, Expression, MainComponent};
2use super::file_definition::{FileID, FileLibrary};
3use super::function_data::{FunctionData, FunctionInfo};
4use super::program_merger::Merger;
5use super::template_data::{TemplateData, TemplateInfo};
6use crate::abstract_syntax_tree::ast::FillMeta;
7use std::collections::HashSet;
8use crate::error_definition::Report;
9
10type Contents = Vec<(FileID, Vec<Definition>)>;
11
12#[derive(Clone)]
13pub struct ProgramArchive {
14 pub id_max: usize,
15 pub file_id_main: FileID,
16 pub file_library: FileLibrary,
17 pub functions: FunctionInfo,
18 pub templates: TemplateInfo,
19 pub function_keys: HashSet<String>,
20 pub template_keys: HashSet<String>,
21 pub public_inputs: Vec<String>,
22 pub initial_template_call: Expression,
23 pub custom_gates: bool,
24}
25impl ProgramArchive {
26 pub fn new(
27 file_library: FileLibrary,
28 file_id_main: FileID,
29 main_component: MainComponent,
30 program_contents: Contents,
31 custom_gates: bool,
32 ) -> Result<ProgramArchive, (FileLibrary, Vec<Report>)> {
33 let mut merger = Merger::new();
34 let mut reports = vec![];
35 for (file_id, definitions) in program_contents {
36 if let Err(mut errs) = merger.add_definitions(file_id, definitions) {
37 reports.append(&mut errs);
38 }
39 }
40 let (mut fresh_id, functions, templates) = merger.decompose();
41 let mut function_keys = HashSet::new();
42 let mut template_keys = HashSet::new();
43 for key in functions.keys() {
44 function_keys.insert(key.clone());
45 }
46 for key in templates.keys() {
47 template_keys.insert(key.clone());
48 }
49 let (public_inputs, mut initial_template_call) = main_component;
50 initial_template_call.fill(file_id_main, &mut fresh_id);
51 if reports.is_empty() {
52 Ok(ProgramArchive {
53 id_max: fresh_id,
54 file_id_main,
55 file_library,
56 functions,
57 templates,
58 public_inputs,
59 initial_template_call,
60 function_keys,
61 template_keys,
62 custom_gates,
63 })
64 } else {
65 Err((file_library, reports))
66 }
67
68 }
69 pub fn get_file_id_main(&self) -> &FileID {
71 &self.file_id_main
72 }
73 pub fn contains_template(&self, template_name: &str) -> bool {
75 self.templates.contains_key(template_name)
76 }
77 pub fn get_template_data(&self, template_name: &str) -> &TemplateData {
78 assert!(self.contains_template(template_name));
79 self.templates.get(template_name).unwrap()
80 }
81 pub fn get_mut_template_data(&mut self, template_name: &str) -> &mut TemplateData {
82 assert!(self.contains_template(template_name));
83 self.templates.get_mut(template_name).unwrap()
84 }
85 pub fn get_template_names(&self) -> &HashSet<String> {
86 &self.template_keys
87 }
88 pub fn get_templates(&self) -> &TemplateInfo {
89 &self.templates
90 }
91 pub fn get_mut_templates(&mut self) -> &mut TemplateInfo {
92 &mut self.templates
93 }
94
95 pub fn remove_template(&mut self, id: &str) {
96 self.template_keys.remove(id);
97 self.templates.remove(id);
98 }
99
100 pub fn contains_function(&self, function_name: &str) -> bool {
102 self.get_functions().contains_key(function_name)
103 }
104 pub fn get_function_data(&self, function_name: &str) -> &FunctionData {
105 assert!(self.contains_function(function_name));
106 self.get_functions().get(function_name).unwrap()
107 }
108 pub fn get_mut_function_data(&mut self, function_name: &str) -> &mut FunctionData {
109 assert!(self.contains_function(function_name));
110 self.functions.get_mut(function_name).unwrap()
111 }
112 pub fn get_function_names(&self) -> &HashSet<String> {
113 &self.function_keys
114 }
115 pub fn get_functions(&self) -> &FunctionInfo {
116 &self.functions
117 }
118 pub fn get_mut_functions(&mut self) -> &mut FunctionInfo {
119 &mut self.functions
120 }
121 pub fn remove_function(&mut self, id: &str) {
122 self.function_keys.remove(id);
123 self.functions.remove(id);
124 }
125
126 pub fn get_public_inputs_main_component(&self) -> &Vec<String> {
128 &self.public_inputs
129 }
130 pub fn get_main_expression(&self) -> &Expression {
131 &self.initial_template_call
132 }
133 pub fn get_file_library(&self) -> &FileLibrary {
135 &self.file_library
136 }
137}