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::Stub;
25use leo_errors::{CompilerError, Handler, Result};
26use leo_passes::*;
27use leo_span::{Symbol, source_map::FileName, with_session_globals};
28
29use snarkvm::prelude::Network;
30
31use indexmap::{IndexMap, IndexSet};
32use std::{
33    fs,
34    path::{Path, PathBuf},
35};
36
37/// The primary entry point of the Leo compiler.
38pub struct Compiler<N: Network> {
39    /// The path to where the compiler outputs all generated files.
40    output_directory: PathBuf,
41    /// The program name,
42    pub program_name: Option<String>,
43    /// Options configuring compilation.
44    compiler_options: CompilerOptions,
45    /// State.
46    state: CompilerState,
47    /// The stubs for imported programs.
48    import_stubs: IndexMap<Symbol, Stub>,
49    /// How many statements were in the AST before DCE?
50    pub statements_before_dce: u32,
51    /// How many statements were in the AST after DCE?
52    pub statements_after_dce: u32,
53    // Allows the compiler to be generic over the network.
54    phantom: std::marker::PhantomData<N>,
55}
56
57impl<N: Network> Compiler<N> {
58    pub fn parse(&mut self, source: &str, filename: FileName) -> Result<()> {
59        // Register the source in the source map.
60        let source_file = with_session_globals(|s| s.source_map.new_source(source, filename));
61
62        // Use the parser to construct the abstract syntax tree (ast).
63        self.state.ast = leo_parser::parse_ast::<N>(
64            self.state.handler.clone(),
65            &self.state.node_builder,
66            &source_file.src,
67            source_file.absolute_start,
68        )?;
69
70        // Check that the name of its program scope matches the expected name.
71        // Note that parsing enforces that there is exactly one program scope in a file.
72        let program_scope = self.state.ast.ast.program_scopes.values().next().unwrap();
73        if self.program_name.is_none() {
74            self.program_name = Some(program_scope.program_id.name.to_string());
75        } else if self.program_name != Some(program_scope.program_id.name.to_string()) {
76            return Err(CompilerError::program_name_should_match_file_name(
77                program_scope.program_id.name,
78                self.program_name.as_ref().unwrap(),
79                program_scope.program_id.name.span,
80            )
81            .into());
82        }
83
84        if self.compiler_options.initial_ast {
85            self.write_ast_to_json("initial.json")?;
86            self.write_ast("initial.ast")?;
87        }
88
89        Ok(())
90    }
91
92    pub fn parse_from_file(&mut self, source_file_path: impl AsRef<Path>) -> Result<()> {
93        // Load the program file.
94        let source = fs::read_to_string(&source_file_path)
95            .map_err(|e| CompilerError::file_read_error(source_file_path.as_ref().display().to_string(), e))?;
96        self.parse(&source, FileName::Real(source_file_path.as_ref().into()))
97    }
98
99    /// Returns a new Leo compiler.
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    ) -> Self {
108        Self {
109            state: CompilerState { handler, is_test, ..Default::default() },
110            output_directory,
111            program_name: expected_program_name,
112            compiler_options: compiler_options.unwrap_or_default(),
113            import_stubs,
114            statements_before_dce: 0,
115            statements_after_dce: 0,
116            phantom: Default::default(),
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        self.do_pass::<SymbolTableCreation>(())?;
139
140        self.do_pass::<TypeChecking>(TypeCheckingInput {
141            max_array_elements: N::MAX_ARRAY_ELEMENTS,
142            max_mappings: N::MAX_MAPPINGS,
143            max_functions: N::MAX_FUNCTIONS,
144        })?;
145
146        self.do_pass::<ProcessingScript>(())?;
147
148        self.do_pass::<StaticAnalyzing>(())?;
149
150        self.do_pass::<ConstPropagationAndUnrolling>(())?;
151
152        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: true })?;
153
154        self.do_pass::<Destructuring>(())?;
155
156        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: false })?;
157
158        self.do_pass::<WriteTransforming>(())?;
159
160        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: false })?;
161
162        self.do_pass::<Flattening>(())?;
163
164        self.do_pass::<FunctionInlining>(())?;
165
166        let output = self.do_pass::<DeadCodeEliminating>(())?;
167        self.statements_before_dce = output.statements_before;
168        self.statements_after_dce = output.statements_after;
169
170        Ok(())
171    }
172
173    /// Returns a compiled Leo program.
174    pub fn compile(&mut self, source: &str, filename: FileName) -> Result<String> {
175        // Parse the program.
176        self.parse(source, filename)?;
177        // Merge the stubs into the AST.
178        self.add_import_stubs()?;
179        // Run the intermediate compiler stages.
180        self.intermediate_passes()?;
181        // Run code generation.
182        let bytecode = CodeGenerating::do_pass((), &mut self.state)?;
183        Ok(bytecode)
184    }
185
186    pub fn compile_from_file(&mut self, source_file_path: impl AsRef<Path>) -> Result<String> {
187        let source = fs::read_to_string(&source_file_path)
188            .map_err(|e| CompilerError::file_read_error(source_file_path.as_ref().display().to_string(), e))?;
189        self.compile(&source, FileName::Real(source_file_path.as_ref().into()))
190    }
191
192    /// Writes the AST to a JSON file.
193    fn write_ast_to_json(&self, file_suffix: &str) -> Result<()> {
194        // Remove `Span`s if they are not enabled.
195        if self.compiler_options.ast_spans_enabled {
196            self.state.ast.to_json_file(
197                self.output_directory.clone(),
198                &format!("{}.{file_suffix}", self.program_name.as_ref().unwrap()),
199            )?;
200        } else {
201            self.state.ast.to_json_file_without_keys(
202                self.output_directory.clone(),
203                &format!("{}.{file_suffix}", self.program_name.as_ref().unwrap()),
204                &["_span", "span"],
205            )?;
206        }
207        Ok(())
208    }
209
210    /// Writes the AST to a file (Leo syntax, not JSON).
211    fn write_ast(&self, file_suffix: &str) -> Result<()> {
212        let filename = format!("{}.{file_suffix}", self.program_name.as_ref().unwrap());
213        let full_filename = self.output_directory.join(&filename);
214        let contents = self.state.ast.ast.to_string();
215        fs::write(&full_filename, contents).map_err(|e| CompilerError::failed_ast_file(full_filename.display(), e))?;
216        Ok(())
217    }
218
219    /// Merge the imported stubs which are dependencies of the current program into the AST
220    /// in topological order.
221    pub fn add_import_stubs(&mut self) -> Result<()> {
222        let mut explored = IndexSet::<Symbol>::new();
223        let mut to_explore: Vec<Symbol> = self.state.ast.ast.imports.keys().cloned().collect();
224
225        while let Some(import) = to_explore.pop() {
226            explored.insert(import);
227            if let Some(stub) = self.import_stubs.get(&import) {
228                for new_import_id in stub.imports.iter() {
229                    if !explored.contains(&new_import_id.name.name) {
230                        to_explore.push(new_import_id.name.name);
231                    }
232                }
233            } else {
234                return Err(CompilerError::imported_program_not_found(
235                    self.program_name.as_ref().unwrap(),
236                    import,
237                    self.state.ast.ast.imports[&import].1,
238                )
239                .into());
240            }
241        }
242
243        // Iterate in the order of `import_stubs` to make sure they
244        // stay topologically sorted.
245        self.state.ast.ast.stubs = self
246            .import_stubs
247            .iter()
248            .filter(|(symbol, _stub)| explored.contains(*symbol))
249            .map(|(symbol, stub)| (*symbol, stub.clone()))
250            .collect();
251        Ok(())
252    }
253}