leo_compiler/
compiler.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17//! The compiler for Leo programs.
18//!
19//! The [`Compiler`] type compiles Leo programs into R1CS circuits.
20
21use crate::{AstSnapshots, CompilerOptions};
22
23pub use leo_ast::Ast;
24use leo_ast::{NetworkName, Stub};
25use leo_errors::{CompilerError, Handler, Result};
26use leo_passes::*;
27use leo_span::{Symbol, source_map::FileName, with_session_globals};
28
29use std::{
30    ffi::OsStr,
31    fs,
32    path::{Path, PathBuf},
33};
34
35use indexmap::{IndexMap, IndexSet};
36use walkdir::WalkDir;
37
38/// The primary entry point of the Leo compiler.
39pub struct Compiler {
40    /// The path to where the compiler outputs all generated files.
41    output_directory: PathBuf,
42    /// The program name,
43    pub program_name: Option<String>,
44    /// Options configuring compilation.
45    compiler_options: CompilerOptions,
46    /// State.
47    state: CompilerState,
48    /// The stubs for imported programs.
49    import_stubs: IndexMap<Symbol, Stub>,
50    /// How many statements were in the AST before DCE?
51    pub statements_before_dce: u32,
52    /// How many statements were in the AST after DCE?
53    pub statements_after_dce: u32,
54}
55
56impl Compiler {
57    pub fn parse(&mut self, source: &str, filename: FileName, modules: &[(&str, FileName)]) -> Result<()> {
58        // Register the source in the source map.
59        let source_file = with_session_globals(|s| s.source_map.new_source(source, filename));
60
61        // Register the sources of all the modules in the source map.
62        let modules = modules
63            .iter()
64            .map(|(source, filename)| with_session_globals(|s| s.source_map.new_source(source, filename.clone())))
65            .collect::<Vec<_>>();
66
67        // Use the parser to construct the abstract syntax tree (ast).
68        self.state.ast = leo_parser::parse_ast(
69            self.state.handler.clone(),
70            &self.state.node_builder,
71            &source_file,
72            &modules,
73            self.state.network,
74        )?;
75
76        // Check that the name of its program scope matches the expected name.
77        // Note that parsing enforces that there is exactly one program scope in a file.
78        let program_scope = self.state.ast.ast.program_scopes.values().next().unwrap();
79        if self.program_name.is_none() {
80            self.program_name = Some(program_scope.program_id.name.to_string());
81        } else if self.program_name != Some(program_scope.program_id.name.to_string()) {
82            return Err(CompilerError::program_name_should_match_file_name(
83                program_scope.program_id.name,
84                self.program_name.as_ref().unwrap(),
85                program_scope.program_id.name.span,
86            )
87            .into());
88        }
89
90        if self.compiler_options.initial_ast {
91            self.write_ast_to_json("initial.json")?;
92            self.write_ast("initial.ast")?;
93        }
94
95        Ok(())
96    }
97
98    /// Returns a new Leo compiler.
99    #[allow(clippy::too_many_arguments)]
100    pub fn new(
101        expected_program_name: Option<String>,
102        is_test: bool,
103        handler: Handler,
104        output_directory: PathBuf,
105        compiler_options: Option<CompilerOptions>,
106        import_stubs: IndexMap<Symbol, Stub>,
107        network: NetworkName,
108    ) -> Self {
109        Self {
110            state: CompilerState { handler, is_test, network, ..Default::default() },
111            output_directory,
112            program_name: expected_program_name,
113            compiler_options: compiler_options.unwrap_or_default(),
114            import_stubs,
115            statements_before_dce: 0,
116            statements_after_dce: 0,
117        }
118    }
119
120    fn do_pass<P: Pass>(&mut self, input: P::Input) -> Result<P::Output> {
121        let output = P::do_pass(input, &mut self.state)?;
122
123        let write = match &self.compiler_options.ast_snapshots {
124            AstSnapshots::All => true,
125            AstSnapshots::Some(passes) => passes.contains(P::NAME),
126        };
127
128        if write {
129            self.write_ast_to_json(&format!("{}.json", P::NAME))?;
130            self.write_ast(&format!("{}.ast", P::NAME))?;
131        }
132
133        Ok(output)
134    }
135
136    /// Runs the compiler stages.
137    pub fn intermediate_passes(&mut self) -> Result<()> {
138        let type_checking_config = TypeCheckingInput::new(self.state.network);
139
140        self.do_pass::<PathResolution>(())?;
141
142        self.do_pass::<SymbolTableCreation>(())?;
143
144        self.do_pass::<TypeChecking>(type_checking_config.clone())?;
145
146        self.do_pass::<ProcessingAsync>(type_checking_config.clone())?;
147
148        self.do_pass::<StaticAnalyzing>(())?;
149
150        self.do_pass::<ConstPropUnrollAndMorphing>(type_checking_config.clone())?;
151
152        self.do_pass::<OptionLowering>(type_checking_config)?;
153
154        self.do_pass::<ProcessingScript>(())?;
155
156        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: true })?;
157
158        self.do_pass::<Destructuring>(())?;
159
160        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: false })?;
161
162        self.do_pass::<WriteTransforming>(())?;
163
164        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: false })?;
165
166        self.do_pass::<Flattening>(())?;
167
168        self.do_pass::<FunctionInlining>(())?;
169
170        // Flattening may produce ternary expressions not in SSA form. In addition,
171        // inlining may create shadowed variables, so rename definitions.
172        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: true })?;
173
174        self.do_pass::<CommonSubexpressionEliminating>(())?;
175
176        let output = self.do_pass::<DeadCodeEliminating>(())?;
177        self.statements_before_dce = output.statements_before;
178        self.statements_after_dce = output.statements_after;
179
180        Ok(())
181    }
182
183    /// Compiles a program from a given source string and a list of module sources.
184    ///
185    /// # Arguments
186    ///
187    /// * `source` - The main source code as a string slice.
188    /// * `filename` - The name of the main source file.
189    /// * `modules` - A vector of tuples where each tuple contains:
190    ///     - A module source as a string slice.
191    ///     - Its associated `FileName`.
192    ///
193    /// # Returns
194    ///
195    /// * `Ok(String)` containing the generated bytecode if compilation succeeds.
196    /// * `Err(CompilerError)` if any stage of the pipeline fails.
197    pub fn compile(&mut self, source: &str, filename: FileName, modules: &Vec<(&str, FileName)>) -> Result<String> {
198        // Parse the program.
199        self.parse(source, filename, modules)?;
200        // Merge the stubs into the AST.
201        self.add_import_stubs()?;
202        // Run the intermediate compiler stages.
203        self.intermediate_passes()?;
204        // Run code generation.
205        let bytecode = CodeGenerating::do_pass((), &mut self.state)?;
206        Ok(bytecode)
207    }
208
209    /// Compiles a program from a source file and its associated module files in the same directory tree.
210    ///
211    /// This method reads the main source file and collects all other source files under the same
212    /// root directory (excluding the main file itself). It assumes a modular structure where additional
213    /// source files are compiled as modules, with deeper files (submodules) compiled first.
214    ///
215    /// # Arguments
216    ///
217    /// * `source_file_path` - A path to the main source file to compile. It must have a parent directory,
218    ///   which is used as the root for discovering additional module files.
219    ///
220    /// # Returns
221    ///
222    /// * `Ok(String)` containing the compiled output if successful.
223    /// * `Err(CompilerError)` if reading the main file fails or a compilation error occurs.
224    ///
225    /// # Panics
226    ///
227    /// * If the provided source file has no parent directory.
228    /// * If any discovered module file cannot be read (marked as a TODO).
229    pub fn compile_from_directory(
230        &mut self,
231        entry_file_path: impl AsRef<Path>,
232        source_directory: impl AsRef<Path>,
233    ) -> Result<String> {
234        // Read the contents of the main source file.
235        let source = fs::read_to_string(&entry_file_path)
236            .map_err(|e| CompilerError::file_read_error(entry_file_path.as_ref().display().to_string(), e))?;
237
238        // Walk all files under source_directory recursively, excluding the main source file itself.
239        let files = WalkDir::new(source_directory)
240            .into_iter()
241            .filter_map(Result::ok)
242            .filter(|e| {
243                e.file_type().is_file()
244                    && e.path() != entry_file_path.as_ref()
245                    && e.path().extension() == Some(OsStr::new("leo"))
246            })
247            .collect::<Vec<_>>();
248
249        let mut module_sources = Vec::new(); // Keep Strings alive for valid borrowing
250        let mut modules = Vec::new(); // Parsed (source, filename) tuples for compilation
251
252        // Read all module files and store their contents
253        for file in &files {
254            let source = fs::read_to_string(file.path())
255                .map_err(|e| CompilerError::file_read_error(file.path().display().to_string(), e))?;
256            module_sources.push(source); // Keep the String alive
257        }
258
259        // Create tuples of (&str, FileName) for the compiler
260        for (i, file) in files.iter().enumerate() {
261            let source = &module_sources[i]; // Borrow from the alive String
262            modules.push((&source[..], FileName::Real(file.path().into())));
263        }
264
265        // Compile the main source along with all collected modules
266        self.compile(&source, FileName::Real(entry_file_path.as_ref().into()), &modules)
267    }
268
269    /// Writes the AST to a JSON file.
270    fn write_ast_to_json(&self, file_suffix: &str) -> Result<()> {
271        // Remove `Span`s if they are not enabled.
272        if self.compiler_options.ast_spans_enabled {
273            self.state.ast.to_json_file(
274                self.output_directory.clone(),
275                &format!("{}.{file_suffix}", self.program_name.as_ref().unwrap()),
276            )?;
277        } else {
278            self.state.ast.to_json_file_without_keys(
279                self.output_directory.clone(),
280                &format!("{}.{file_suffix}", self.program_name.as_ref().unwrap()),
281                &["_span", "span"],
282            )?;
283        }
284        Ok(())
285    }
286
287    /// Writes the AST to a file (Leo syntax, not JSON).
288    fn write_ast(&self, file_suffix: &str) -> Result<()> {
289        let filename = format!("{}.{file_suffix}", self.program_name.as_ref().unwrap());
290        let full_filename = self.output_directory.join(&filename);
291        let contents = self.state.ast.ast.to_string();
292        fs::write(&full_filename, contents).map_err(|e| CompilerError::failed_ast_file(full_filename.display(), e))?;
293        Ok(())
294    }
295
296    /// Merge the imported stubs which are dependencies of the current program into the AST
297    /// in topological order.
298    pub fn add_import_stubs(&mut self) -> Result<()> {
299        let mut explored = IndexSet::<Symbol>::new();
300        let mut to_explore: Vec<Symbol> = self.state.ast.ast.imports.keys().cloned().collect();
301
302        while let Some(import) = to_explore.pop() {
303            explored.insert(import);
304            if let Some(stub) = self.import_stubs.get(&import) {
305                for new_import_id in stub.imports.iter() {
306                    if !explored.contains(&new_import_id.name.name) {
307                        to_explore.push(new_import_id.name.name);
308                    }
309                }
310            } else {
311                return Err(CompilerError::imported_program_not_found(
312                    self.program_name.as_ref().unwrap(),
313                    import,
314                    self.state.ast.ast.imports[&import].1,
315                )
316                .into());
317            }
318        }
319
320        // Iterate in the order of `import_stubs` to make sure they
321        // stay topologically sorted.
322        self.state.ast.ast.stubs = self
323            .import_stubs
324            .iter()
325            .filter(|(symbol, _stub)| explored.contains(*symbol))
326            .map(|(symbol, stub)| (*symbol, stub.clone()))
327            .collect();
328        Ok(())
329    }
330}