Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to compile prepared db with debug info #5157

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions crates/cairo-lang-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn compile_cairo_project_at_path(
) -> Result<Program> {
let mut db = RootDatabase::builder().detect_corelib().build()?;
let main_crate_ids = setup_project(&mut db, path)?;
compile_prepared_db(&mut db, main_crate_ids, compiler_config)
compile_prepared_db_program(&mut db, main_crate_ids, compiler_config)
}

/// Compiles a Cairo project.
Expand All @@ -79,7 +79,7 @@ pub fn compile(
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(&mut db, main_crate_ids, compiler_config)
compile_prepared_db_program(&mut db, main_crate_ids, compiler_config)
}

/// Runs Cairo compiler.
Expand All @@ -93,22 +93,44 @@ pub fn compile(
/// # 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
/// `db.intern_crate(CrateLongId::Real(name))` 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<Program> {
) -> Result<SierraProgramWithDebug> {
compiler_config.diagnostics_reporter.ensure(db)?;

let SierraProgramWithDebug { program: mut sierra_program, .. } = arc_unwrap_or_clone(
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 = replace_sierra_ids_in_program(db, &sierra_program);
sierra_program_with_debug.program =
replace_sierra_ids_in_program(db, &sierra_program_with_debug.program);
}

Ok(sierra_program)
Ok(sierra_program_with_debug)
}