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)?;
151
152        self.do_pass::<ProcessingScript>(())?;
153
154        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: true })?;
155
156        self.do_pass::<Destructuring>(())?;
157
158        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: false })?;
159
160        self.do_pass::<WriteTransforming>(())?;
161
162        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: false })?;
163
164        self.do_pass::<Flattening>(())?;
165
166        self.do_pass::<FunctionInlining>(())?;
167
168        let output = self.do_pass::<DeadCodeEliminating>(())?;
169        self.statements_before_dce = output.statements_before;
170        self.statements_after_dce = output.statements_after;
171
172        Ok(())
173    }
174
175    /// Compiles a program from a given source string and a list of module sources.
176    ///
177    /// # Arguments
178    ///
179    /// * `source` - The main source code as a string slice.
180    /// * `filename` - The name of the main source file.
181    /// * `modules` - A vector of tuples where each tuple contains:
182    ///     - A module source as a string slice.
183    ///     - Its associated `FileName`.
184    ///
185    /// # Returns
186    ///
187    /// * `Ok(String)` containing the generated bytecode if compilation succeeds.
188    /// * `Err(CompilerError)` if any stage of the pipeline fails.
189    pub fn compile(&mut self, source: &str, filename: FileName, modules: &Vec<(&str, FileName)>) -> Result<String> {
190        // Parse the program.
191        self.parse(source, filename, modules)?;
192        // Merge the stubs into the AST.
193        self.add_import_stubs()?;
194        // Run the intermediate compiler stages.
195        self.intermediate_passes()?;
196        // Run code generation.
197        let bytecode = CodeGenerating::do_pass((), &mut self.state)?;
198        Ok(bytecode)
199    }
200
201    /// Compiles a program from a source file and its associated module files in the same directory tree.
202    ///
203    /// This method reads the main source file and collects all other source files under the same
204    /// root directory (excluding the main file itself). It assumes a modular structure where additional
205    /// source files are compiled as modules, with deeper files (submodules) compiled first.
206    ///
207    /// # Arguments
208    ///
209    /// * `source_file_path` - A path to the main source file to compile. It must have a parent directory,
210    ///   which is used as the root for discovering additional module files.
211    ///
212    /// # Returns
213    ///
214    /// * `Ok(String)` containing the compiled output if successful.
215    /// * `Err(CompilerError)` if reading the main file fails or a compilation error occurs.
216    ///
217    /// # Panics
218    ///
219    /// * If the provided source file has no parent directory.
220    /// * If any discovered module file cannot be read (marked as a TODO).
221    pub fn compile_from_directory(
222        &mut self,
223        entry_file_path: impl AsRef<Path>,
224        source_directory: impl AsRef<Path>,
225    ) -> Result<String> {
226        // Read the contents of the main source file.
227        let source = fs::read_to_string(&entry_file_path)
228            .map_err(|e| CompilerError::file_read_error(entry_file_path.as_ref().display().to_string(), e))?;
229
230        // Walk all files under source_directory recursively, excluding the main source file itself.
231        let files = WalkDir::new(source_directory)
232            .into_iter()
233            .filter_map(Result::ok)
234            .filter(|e| {
235                e.file_type().is_file()
236                    && e.path() != entry_file_path.as_ref()
237                    && e.path().extension() == Some(OsStr::new("leo"))
238            })
239            .collect::<Vec<_>>();
240
241        let mut module_sources = Vec::new(); // Keep Strings alive for valid borrowing
242        let mut modules = Vec::new(); // Parsed (source, filename) tuples for compilation
243
244        // Read all module files and store their contents
245        for file in &files {
246            let source = fs::read_to_string(file.path())
247                .map_err(|e| CompilerError::file_read_error(file.path().display().to_string(), e))?;
248            module_sources.push(source); // Keep the String alive
249        }
250
251        // Create tuples of (&str, FileName) for the compiler
252        for (i, file) in files.iter().enumerate() {
253            let source = &module_sources[i]; // Borrow from the alive String
254            modules.push((&source[..], FileName::Real(file.path().into())));
255        }
256
257        // Compile the main source along with all collected modules
258        self.compile(&source, FileName::Real(entry_file_path.as_ref().into()), &modules)
259    }
260
261    /// Writes the AST to a JSON file.
262    fn write_ast_to_json(&self, file_suffix: &str) -> Result<()> {
263        // Remove `Span`s if they are not enabled.
264        if self.compiler_options.ast_spans_enabled {
265            self.state.ast.to_json_file(
266                self.output_directory.clone(),
267                &format!("{}.{file_suffix}", self.program_name.as_ref().unwrap()),
268            )?;
269        } else {
270            self.state.ast.to_json_file_without_keys(
271                self.output_directory.clone(),
272                &format!("{}.{file_suffix}", self.program_name.as_ref().unwrap()),
273                &["_span", "span"],
274            )?;
275        }
276        Ok(())
277    }
278
279    /// Writes the AST to a file (Leo syntax, not JSON).
280    fn write_ast(&self, file_suffix: &str) -> Result<()> {
281        let filename = format!("{}.{file_suffix}", self.program_name.as_ref().unwrap());
282        let full_filename = self.output_directory.join(&filename);
283        let contents = self.state.ast.ast.to_string();
284        fs::write(&full_filename, contents).map_err(|e| CompilerError::failed_ast_file(full_filename.display(), e))?;
285        Ok(())
286    }
287
288    /// Merge the imported stubs which are dependencies of the current program into the AST
289    /// in topological order.
290    pub fn add_import_stubs(&mut self) -> Result<()> {
291        let mut explored = IndexSet::<Symbol>::new();
292        let mut to_explore: Vec<Symbol> = self.state.ast.ast.imports.keys().cloned().collect();
293
294        while let Some(import) = to_explore.pop() {
295            explored.insert(import);
296            if let Some(stub) = self.import_stubs.get(&import) {
297                for new_import_id in stub.imports.iter() {
298                    if !explored.contains(&new_import_id.name.name) {
299                        to_explore.push(new_import_id.name.name);
300                    }
301                }
302            } else {
303                return Err(CompilerError::imported_program_not_found(
304                    self.program_name.as_ref().unwrap(),
305                    import,
306                    self.state.ast.ast.imports[&import].1,
307                )
308                .into());
309            }
310        }
311
312        // Iterate in the order of `import_stubs` to make sure they
313        // stay topologically sorted.
314        self.state.ast.ast.stubs = self
315            .import_stubs
316            .iter()
317            .filter(|(symbol, _stub)| explored.contains(*symbol))
318            .map(|(symbol, stub)| (*symbol, stub.clone()))
319            .collect();
320        Ok(())
321    }
322}