1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
//! Cairo compiler.
//!
//! This crate is responsible for compiling a Cairo project into a Sierra program.
//! It is the main entry point for the compiler.
use std::path::Path;
use std::sync::Arc;
use ::cairo_lang_diagnostics::ToOption;
use anyhow::{Context, Result};
use cairo_lang_filesystem::ids::CrateId;
use cairo_lang_lowering::utils::InliningStrategy;
use cairo_lang_sierra::debug_info::{Annotations, DebugInfo};
use cairo_lang_sierra::program::{Program, ProgramArtifact};
use cairo_lang_sierra_generator::db::SierraGenGroup;
use cairo_lang_sierra_generator::executables::{collect_executables, find_executable_function_ids};
use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug;
use cairo_lang_sierra_generator::replace_ids::replace_sierra_ids_in_program;
use crate::db::RootDatabase;
use crate::diagnostics::DiagnosticsReporter;
use crate::project::{get_main_crate_ids_from_project, setup_project, ProjectConfig};
pub mod db;
pub mod diagnostics;
pub mod project;
#[cfg(test)]
mod test;
/// Configuration for the compiler.
#[derive(Default)]
pub struct CompilerConfig<'c> {
pub diagnostics_reporter: DiagnosticsReporter<'c>,
/// Replaces sierra ids with human-readable ones.
pub replace_ids: bool,
/// Disables inlining functions.
pub inlining_strategy: InliningStrategy,
/// The name of the allowed libfuncs list to use in compilation.
/// If None the default list of audited libfuncs will be used.
pub allowed_libfuncs_list_name: Option<String>,
/// Adds mapping used by [cairo-profiler](https://github.com/software-mansion/cairo-profiler) to
/// [cairo_lang_sierra::debug_info::Annotations] in [cairo_lang_sierra::debug_info::DebugInfo].
pub add_statements_functions: bool,
}
/// Compiles a Cairo project at the given path.
/// The project must be a valid Cairo project:
/// Either a standalone `.cairo` file (a single crate), or a directory with a `cairo_project.toml`
/// file.
/// # Arguments
/// * `path` - The path to the project.
/// * `compiler_config` - The compiler configuration.
/// # Returns
/// * `Ok(Program)` - The compiled program.
/// * `Err(anyhow::Error)` - Compilation failed.
pub fn compile_cairo_project_at_path(
path: &Path,
compiler_config: CompilerConfig<'_>,
) -> Result<Program> {
let mut db = RootDatabase::builder()
.with_inlining_strategy(compiler_config.inlining_strategy)
.detect_corelib()
.build()?;
let main_crate_ids = setup_project(&mut db, path)?;
compile_prepared_db_program(&mut db, main_crate_ids, compiler_config)
}
/// Compiles a Cairo project.
/// The project must be a valid Cairo project.
/// This function is a wrapper over [`RootDatabase::builder()`] and [`compile_prepared_db_program`].
/// # Arguments
/// * `project_config` - The project configuration.
/// * `compiler_config` - The compiler configuration.
/// # Returns
/// * `Ok(Program)` - The compiled program.
/// * `Err(anyhow::Error)` - Compilation failed.
pub fn compile(
project_config: ProjectConfig,
compiler_config: CompilerConfig<'_>,
) -> Result<Program> {
let mut db = RootDatabase::builder().with_project_config(project_config.clone()).build()?;
let main_crate_ids = get_main_crate_ids_from_project(&mut db, &project_config);
compile_prepared_db_program(&mut db, main_crate_ids, compiler_config)
}
/// Runs Cairo compiler.
///
/// # Arguments
/// * `db` - Preloaded compilation database.
/// * `main_crate_ids` - [`CrateId`]s to compile. Do not include dependencies here, only pass
/// top-level crates in order to eliminate unused code. Use `CrateLongId::Real(name).intern(db)`
/// in order to obtain [`CrateId`] from its name.
/// * `compiler_config` - The compiler configuration.
/// # Returns
/// * `Ok(Program)` - The compiled program.
/// * `Err(anyhow::Error)` - Compilation failed.
pub fn compile_prepared_db_program(
db: &mut RootDatabase,
main_crate_ids: Vec<CrateId>,
compiler_config: CompilerConfig<'_>,
) -> Result<Program> {
Ok(compile_prepared_db(db, main_crate_ids, compiler_config)?.program)
}
/// Runs Cairo compiler.
///
/// Similar to `compile_prepared_db_program`, but this function returns all the raw debug
/// information.
///
/// # Arguments
/// * `db` - Preloaded compilation database.
/// * `main_crate_ids` - [`CrateId`]s to compile. Do not include dependencies here, only pass
/// top-level crates in order to eliminate unused code. Use `CrateLongId::Real(name).intern(db)`
/// in order to obtain [`CrateId`] from its name.
/// * `compiler_config` - The compiler configuration.
/// # Returns
/// * `Ok(SierraProgramWithDebug)` - The compiled program with debug info.
/// * `Err(anyhow::Error)` - Compilation failed.
pub fn compile_prepared_db(
db: &mut RootDatabase,
main_crate_ids: Vec<CrateId>,
mut compiler_config: CompilerConfig<'_>,
) -> Result<SierraProgramWithDebug> {
compiler_config.diagnostics_reporter.ensure(db)?;
let mut sierra_program_with_debug = Arc::unwrap_or_clone(
db.get_sierra_program(main_crate_ids)
.to_option()
.context("Compilation failed without any diagnostics")?,
);
if compiler_config.replace_ids {
sierra_program_with_debug.program =
replace_sierra_ids_in_program(db, &sierra_program_with_debug.program);
}
Ok(sierra_program_with_debug)
}
/// Runs Cairo compiler.
///
/// Wrapper over [`compile_prepared_db`], but this function returns [`ProgramArtifact`]
/// with requested debug info.
///
/// # Arguments
/// * `db` - Preloaded compilation database.
/// * `main_crate_ids` - [`CrateId`]s to compile. Do not include dependencies here, only pass
/// top-level crates in order to eliminate unused code. Use `CrateLongId::Real(name).intern(db)`
/// in order to obtain [`CrateId`] from its name.
/// * `compiler_config` - The compiler configuration.
/// # Returns
/// * `Ok(ProgramArtifact)` - The compiled program artifact with requested debug info.
/// * `Err(anyhow::Error)` - Compilation failed.
pub fn compile_prepared_db_program_artifact(
db: &mut RootDatabase,
main_crate_ids: Vec<CrateId>,
mut compiler_config: CompilerConfig<'_>,
) -> Result<ProgramArtifact> {
let add_statements_functions = compiler_config.add_statements_functions;
compiler_config.diagnostics_reporter.ensure(db)?;
let executable_functions = find_executable_function_ids(db, main_crate_ids.clone());
let mut sierra_program_with_debug = if executable_functions.is_empty() {
// No executables found - compile for all main crates.
// TODO(maciektr): Deprecate in future. This compilation is useless, without `replace_ids`.
Arc::unwrap_or_clone(
db.get_sierra_program(main_crate_ids)
.to_option()
.context("Compilation failed without any diagnostics")?,
)
} else {
// Compile for executable functions only.
Arc::unwrap_or_clone(
db.get_sierra_program_for_functions(executable_functions.clone().into_keys().collect())
.to_option()
.context("Compilation failed without any diagnostics")?,
)
};
if compiler_config.replace_ids {
sierra_program_with_debug.program =
replace_sierra_ids_in_program(db, &sierra_program_with_debug.program);
}
// Calculate debug info.
let mut debug_info = DebugInfo::default();
if add_statements_functions {
let statements_functions = sierra_program_with_debug
.debug_info
.statements_locations
.extract_statements_functions(db);
debug_info = DebugInfo {
type_names: Default::default(),
libfunc_names: Default::default(),
user_func_names: Default::default(),
annotations: Annotations::from(statements_functions),
executables: Default::default(),
};
}
// Calculate executable function Sierra ids.
let executables =
collect_executables(db, executable_functions, &sierra_program_with_debug.program);
Ok(ProgramArtifact::stripped(sierra_program_with_debug.program)
.with_debug_info(DebugInfo { executables, ..debug_info }))
}