Skip to content
9 changes: 3 additions & 6 deletions crates/lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ use lsp_types::{
};
use noirc_driver::{check_crate, prepare_crate};
use noirc_errors::{DiagnosticKind, FileDiagnostic};
use noirc_frontend::{
graph::{CrateGraph, CrateType},
hir::Context,
};
use noirc_frontend::{graph::CrateGraph, hir::Context};
use serde_json::Value as JsonValue;
use tower::Service;

Expand Down Expand Up @@ -190,7 +187,7 @@ fn on_code_lens_request(
}
};

let crate_id = prepare_crate(&mut context, file_path, CrateType::Binary);
let crate_id = prepare_crate(&mut context, file_path);

// We ignore the warnings and errors produced by compilation for producing codelenses
// because we can still get the test functions even if compilation fails
Expand Down Expand Up @@ -283,7 +280,7 @@ fn on_did_save_text_document(
}
};

let crate_id = prepare_crate(&mut context, file_path, CrateType::Binary);
let crate_id = prepare_crate(&mut context, file_path);

let mut diagnostics = Vec::new();

Expand Down
6 changes: 3 additions & 3 deletions crates/lsp/src/lib_hacky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use lsp_types::{
use noirc_driver::{check_crate, prepare_crate, propagate_dep};
use noirc_errors::{DiagnosticKind, FileDiagnostic};
use noirc_frontend::{
graph::{CrateGraph, CrateId, CrateType},
graph::{CrateGraph, CrateId},
hir::Context,
};

Expand Down Expand Up @@ -286,7 +286,7 @@ fn create_context_at_path(
}
let nargo_toml_path = find_nearest_parent_file(&file_path, &["Nargo.toml"]);

let current_crate_id = prepare_crate(&mut context, &file_path, CrateType::Binary);
let current_crate_id = prepare_crate(&mut context, &file_path);

// TODO(AD): undo hacky dependency resolution
if let Some(nargo_toml_path) = nargo_toml_path {
Expand All @@ -297,7 +297,7 @@ fn create_context_at_path(
.parent()
.unwrap() // TODO
.join(PathBuf::from(&dependency_path).join("src").join("lib.nr"));
let library_crate = prepare_crate(&mut context, &path_to_lib, CrateType::Library);
let library_crate = prepare_crate(&mut context, &path_to_lib);
propagate_dep(&mut context, library_crate, &crate_name.parse().unwrap());
}
}
Expand Down
43 changes: 40 additions & 3 deletions crates/nargo/src/package.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
use std::{collections::BTreeMap, path::PathBuf};
use std::{collections::BTreeMap, fmt::Display, path::PathBuf};

use noirc_frontend::graph::{CrateName, CrateType};
use noirc_frontend::graph::CrateName;

use crate::constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE};

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PackageType {
Library,
Binary,
}

impl Display for PackageType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Library => write!(f, "lib"),
Self::Binary => write!(f, "bin"),
}
}
}

#[derive(Clone)]
pub enum Dependency {
Local { package: Package },
Remote { package: Package },
}

impl Dependency {
pub fn is_binary(&self) -> bool {
match self {
Self::Local { package } | Self::Remote { package } => package.is_binary(),
}
}

pub fn package_name(&self) -> &CrateName {
match self {
Self::Local { package } | Self::Remote { package } => &package.name,
}
}
}

#[derive(Clone)]
pub struct Package {
pub root_dir: PathBuf,
pub crate_type: CrateType,
pub package_type: PackageType,
pub entry_path: PathBuf,
pub name: CrateName,
pub dependencies: BTreeMap<CrateName, Dependency>,
Expand All @@ -30,4 +59,12 @@ impl Package {
// For now it is hard-coded to be toml.
self.root_dir.join(format!("{VERIFIER_INPUT_FILE}.toml"))
}

pub fn is_binary(&self) -> bool {
self.package_type == PackageType::Binary
}

pub fn is_library(&self) -> bool {
self.package_type == PackageType::Library
}
}
52 changes: 29 additions & 23 deletions crates/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::{
errors::CliError, find_package_manifest, manifest::resolve_workspace_from_toml, prepare_package,
errors::{CliError, CompileError},
find_package_manifest,
manifest::resolve_workspace_from_toml,
prepare_package,
};
use acvm::Backend;
use clap::Args;
use iter_extended::btree_map;
use nargo::package::Package;
use noirc_abi::{AbiParameter, AbiType, MAIN_RETURN_NAME};
use noirc_driver::{check_crate, compute_function_signature, CompileOptions};
use noirc_errors::reporter::ReportedErrors;
use noirc_frontend::{
graph::{CrateId, CrateName},
hir::Context,
Expand Down Expand Up @@ -42,33 +44,37 @@ pub(crate) fn run<B: Backend>(
Ok(())
}

fn check_package(
package: &Package,
compile_options: &CompileOptions,
) -> Result<(), ReportedErrors> {
fn check_package(package: &Package, compile_options: &CompileOptions) -> Result<(), CompileError> {
let (mut context, crate_id) = prepare_package(package);
check_crate_and_report_errors(&mut context, crate_id, compile_options.deny_warnings)?;

// XXX: We can have a --overwrite flag to determine if you want to overwrite the Prover/Verifier.toml files
if let Some((parameters, return_type)) = compute_function_signature(&context, &crate_id) {
let path_to_prover_input = package.prover_input_path();
let path_to_verifier_input = package.verifier_input_path();
if package.is_library() {
// Libraries do not have ABIs.
Ok(())
} else {
// XXX: We can have a --overwrite flag to determine if you want to overwrite the Prover/Verifier.toml files
if let Some((parameters, return_type)) = compute_function_signature(&context, &crate_id) {
let path_to_prover_input = package.prover_input_path();
let path_to_verifier_input = package.verifier_input_path();

// If they are not available, then create them and populate them based on the ABI
if !path_to_prover_input.exists() {
let prover_toml = create_input_toml_template(parameters.clone(), None);
write_to_file(prover_toml.as_bytes(), &path_to_prover_input);
}
if !path_to_verifier_input.exists() {
let public_inputs =
parameters.into_iter().filter(|param| param.is_public()).collect();

// If they are not available, then create them and populate them based on the ABI
if !path_to_prover_input.exists() {
let prover_toml = create_input_toml_template(parameters.clone(), None);
write_to_file(prover_toml.as_bytes(), &path_to_prover_input);
}
if !path_to_verifier_input.exists() {
let public_inputs = parameters.into_iter().filter(|param| param.is_public()).collect();
let verifier_toml = create_input_toml_template(public_inputs, return_type);
write_to_file(verifier_toml.as_bytes(), &path_to_verifier_input);
}

let verifier_toml = create_input_toml_template(public_inputs, return_type);
write_to_file(verifier_toml.as_bytes(), &path_to_verifier_input);
Ok(())
} else {
Err(CompileError::MissingMainFunction(package.name.clone()))
}
} else {
// This means that this is a library. Libraries do not have ABIs.
}
Ok(())
}

/// Generates the contents of a toml file with fields for each of the passed parameters.
Expand Down Expand Up @@ -223,7 +229,7 @@ pub(crate) fn check_crate_and_report_errors(
context: &mut Context,
crate_id: CrateId,
deny_warnings: bool,
) -> Result<(), ReportedErrors> {
) -> Result<(), CompileError> {
let result = check_crate(context, crate_id, deny_warnings).map(|warnings| ((), warnings));
super::compile_cmd::report_errors(result, context, deny_warnings)
}
26 changes: 14 additions & 12 deletions crates/nargo_cli/src/cli/codegen_verifier_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use std::path::PathBuf;

use super::fs::{
common_reference_string::{
read_cached_common_reference_string, update_common_reference_string,
write_cached_common_reference_string,
use super::NargoConfig;
use super::{
compile_cmd::compile_package,
fs::{
common_reference_string::{
read_cached_common_reference_string, update_common_reference_string,
write_cached_common_reference_string,
},
create_named_dir,
program::read_program_from_file,
write_to_file,
},
create_named_dir,
program::read_program_from_file,
write_to_file,
};
use super::NargoConfig;
use crate::{cli::compile_cmd::compile_circuit, errors::CliError};
use crate::{find_package_manifest, manifest::resolve_workspace_from_toml, prepare_package};
use crate::errors::CliError;
use crate::{find_package_manifest, manifest::resolve_workspace_from_toml};
use acvm::Backend;
use clap::Args;
use nargo::{
Expand Down Expand Up @@ -75,8 +78,7 @@ fn smart_contract_for_package<B: Backend>(
.map_err(CliError::CommonReferenceStringError)?;
(common_reference_string, program)
} else {
let (mut context, crate_id) = prepare_package(package);
let program = compile_circuit(backend, &mut context, crate_id, compile_options)?;
let (_, program) = compile_package(backend, package, compile_options)?;
let common_reference_string =
update_common_reference_string(backend, &common_reference_string, &program.circuit)
.map_err(CliError::CommonReferenceStringError)?;
Expand Down
29 changes: 16 additions & 13 deletions crates/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ use acvm::acir::circuit::OpcodeLabel;
use acvm::{acir::circuit::Circuit, Backend};
use iter_extended::try_vecmap;
use iter_extended::vecmap;
use nargo::package::Package;
use nargo::{artifacts::contract::PreprocessedContract, NargoError};
use noirc_driver::{
compile_contracts, compile_main, CompileOptions, CompiledProgram, ErrorsAndWarnings, Warnings,
};
use noirc_errors::reporter::ReportedErrors;
use noirc_frontend::graph::{CrateId, CrateName};
use noirc_frontend::graph::CrateName;
use noirc_frontend::hir::Context;

use clap::Args;

use nargo::ops::{preprocess_contract_function, preprocess_program};

use crate::errors::CliError;
use crate::errors::{CliError, CompileError};
use crate::manifest::resolve_workspace_from_toml;
use crate::{find_package_manifest, prepare_package};

Expand Down Expand Up @@ -108,8 +108,7 @@ pub(crate) fn run<B: Backend>(
}
} else {
for package in &workspace {
let (mut context, crate_id) = prepare_package(package);
let program = compile_circuit(backend, &mut context, crate_id, &args.compile_options)?;
let (_, program) = compile_package(backend, package, &args.compile_options)?;

common_reference_string =
update_common_reference_string(backend, &common_reference_string, &program.circuit)
Expand All @@ -127,14 +126,18 @@ pub(crate) fn run<B: Backend>(
Ok(())
}

pub(crate) fn compile_circuit<B: Backend>(
pub(crate) fn compile_package<B: Backend>(
backend: &B,
context: &mut Context,
crate_id: CrateId,
package: &Package,
compile_options: &CompileOptions,
) -> Result<CompiledProgram, ReportedErrors> {
let result = compile_main(context, crate_id, compile_options);
let mut program = report_errors(result, context, compile_options.deny_warnings)?;
) -> Result<(Context, CompiledProgram), CompileError> {
if package.is_library() {
return Err(CompileError::LibraryCrate(package.name.clone()));
}

let (mut context, crate_id) = prepare_package(package);
let result = compile_main(&mut context, crate_id, compile_options);
let mut program = report_errors(result, &context, compile_options.deny_warnings)?;
// Apply backend specific optimizations.
let (optimized_circuit, opcode_labels) = optimize_circuit(backend, program.circuit)
.expect("Backend does not support an opcode that is in the IR");
Expand All @@ -150,7 +153,7 @@ pub(crate) fn compile_circuit<B: Backend>(
});
program.debug.update_acir(opcode_ids);

Ok(program)
Ok((context, program))
}

pub(super) fn optimize_circuit<B: Backend>(
Expand All @@ -171,7 +174,7 @@ pub(crate) fn report_errors<T>(
result: Result<(T, Warnings), ErrorsAndWarnings>,
context: &Context,
deny_warnings: bool,
) -> Result<T, ReportedErrors> {
) -> Result<T, CompileError> {
let (t, warnings) = result.map_err(|errors| {
noirc_errors::reporter::report_all(&context.file_manager, &errors, deny_warnings)
})?;
Expand Down
7 changes: 3 additions & 4 deletions crates/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use noirc_errors::{debug_info::DebugInfo, CustomDiagnostic};
use noirc_frontend::graph::CrateName;
use noirc_frontend::hir::Context;

use super::compile_cmd::compile_circuit;
use super::compile_cmd::compile_package;
use super::fs::{inputs::read_inputs_from_file, witness::save_witness_to_dir};
use super::NargoConfig;
use crate::errors::CliError;
use crate::find_package_manifest;
use crate::manifest::resolve_workspace_from_toml;
use crate::{find_package_manifest, prepare_package};

/// Executes a circuit to calculate its return value
#[derive(Debug, Clone, Args)]
Expand Down Expand Up @@ -69,8 +69,7 @@ fn execute_package<B: Backend>(
prover_name: &str,
compile_options: &CompileOptions,
) -> Result<(Option<InputValue>, WitnessMap), CliError<B>> {
let (mut context, crate_id) = prepare_package(package);
let compiled_program = compile_circuit(backend, &mut context, crate_id, compile_options)?;
let (context, compiled_program) = compile_package(backend, package, compile_options)?;
let CompiledProgram { abi, circuit, debug } = compiled_program;

// Parse the initial witness values from Prover.toml
Expand Down
7 changes: 3 additions & 4 deletions crates/nargo_cli/src/cli/info_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use noirc_driver::CompileOptions;
use noirc_frontend::graph::CrateName;

use crate::{
cli::compile_cmd::compile_circuit, errors::CliError, find_package_manifest,
manifest::resolve_workspace_from_toml, prepare_package,
cli::compile_cmd::compile_package, errors::CliError, find_package_manifest,
manifest::resolve_workspace_from_toml,
};

use super::NargoConfig;
Expand Down Expand Up @@ -46,8 +46,7 @@ fn count_opcodes_and_gates_in_package<B: Backend>(
package: &Package,
compile_options: &CompileOptions,
) -> Result<(), CliError<B>> {
let (mut context, crate_id) = prepare_package(package);
let compiled_program = compile_circuit(backend, &mut context, crate_id, compile_options)?;
let (_, compiled_program) = compile_package(backend, package, compile_options)?;

let num_opcodes = compiled_program.circuit.opcodes.len();

Expand Down
Loading