Skip to content

Commit

Permalink
chore: remove pub wildcard import of ast into noirc_frontend root (#…
Browse files Browse the repository at this point in the history
…4862)

# Description

## Problem\*

Related to #4852 

## Summary\*

This PR removes the wildcard import of the `ast` module into the root of
the `noirc_frontend` crate. This encourages us to be more qualified
usage of the ast to make it clearer about which we're working with.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Apr 22, 2024
1 parent 3614d00 commit 1969ce3
Show file tree
Hide file tree
Showing 73 changed files with 263 additions and 243 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use noirc_errors::{Location, Span};
use noirc_frontend::ast::{FunctionReturnType, NoirFunction, UnresolvedTypeData};
use noirc_frontend::{
graph::CrateId,
macros_api::{FileId, HirContext},
parse_program, FunctionReturnType, NoirFunction, Type, UnresolvedTypeData,
parse_program, Type,
};

use crate::utils::{
Expand Down
5 changes: 3 additions & 2 deletions aztec_macros/src/transforms/contract_interface.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use noirc_frontend::ast::{NoirFunction, UnresolvedTypeData};
use noirc_frontend::{
graph::CrateId,
macros_api::{FileId, HirContext, HirExpression, HirLiteral, HirStatement},
parse_program,
parser::SortedModule,
NoirFunction, Type, UnresolvedTypeData,
Type,
};

use crate::utils::{
Expand Down Expand Up @@ -52,7 +53,7 @@ pub fn stub_function(aztec_visibility: &str, func: &NoirFunction) -> String {
})
.collect::<Vec<_>>()
.join(", ");
let fn_return_type: noirc_frontend::UnresolvedType = func.return_type();
let fn_return_type: noirc_frontend::ast::UnresolvedType = func.return_type();

let fn_selector = format!("dep::aztec::protocol_types::abis::function_selector::FunctionSelector::from_signature(\"{}\")", SELECTOR_PLACEHOLDER);

Expand Down
6 changes: 4 additions & 2 deletions aztec_macros/src/transforms/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use iter_extended::vecmap;
use noirc_errors::Span;
use noirc_frontend::ast::{
ExpressionKind, FunctionDefinition, FunctionReturnType, ItemVisibility, Literal, NoirFunction,
Visibility,
};
use noirc_frontend::{
graph::CrateId,
macros_api::{
Expand All @@ -8,8 +12,6 @@ use noirc_frontend::{
UnresolvedTypeData,
},
token::SecondaryAttribute,
ExpressionKind, FunctionDefinition, FunctionReturnType, ItemVisibility, Literal, NoirFunction,
Visibility,
};

use crate::{
Expand Down
15 changes: 9 additions & 6 deletions aztec_macros/src/transforms/functions.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use convert_case::{Case, Casing};
use noirc_errors::Span;
use noirc_frontend::{
macros_api::FieldElement, parse_program, BlockExpression, ConstrainKind, ConstrainStatement,
Distinctness, Expression, ExpressionKind, ForLoopStatement, ForRange, FunctionReturnType,
Ident, Literal, NoirFunction, NoirStruct, Param, PathKind, Pattern, Signedness, Statement,
StatementKind, UnresolvedType, UnresolvedTypeData, Visibility,
use noirc_frontend::ast;
use noirc_frontend::ast::{
BlockExpression, ConstrainKind, ConstrainStatement, Distinctness, Expression, ExpressionKind,
ForLoopStatement, ForRange, FunctionReturnType, Ident, Literal, NoirFunction, NoirStruct,
Param, PathKind, Pattern, Signedness, Statement, StatementKind, UnresolvedType,
UnresolvedTypeData, Visibility,
};

use noirc_frontend::{macros_api::FieldElement, parse_program};

use crate::{
chained_dep, chained_path,
utils::{
Expand Down Expand Up @@ -334,7 +337,7 @@ fn serialize_to_hasher(
&UnresolvedType {
typ: UnresolvedTypeData::Integer(
Signedness::Unsigned,
noirc_frontend::IntegerBitSize::ThirtyTwo,
ast::IntegerBitSize::ThirtyTwo,
),
span: None,
},
Expand Down
8 changes: 6 additions & 2 deletions aztec_macros/src/transforms/note_interface.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use noirc_errors::Span;
use noirc_frontend::ast::{
ItemVisibility, LetStatement, NoirFunction, NoirStruct, PathKind, TraitImplItem, TypeImpl,
UnresolvedTypeData, UnresolvedTypeExpression,
};
use noirc_frontend::{
graph::CrateId,
macros_api::{FileId, HirContext, HirExpression, HirLiteral, HirStatement},
parse_program,
parser::SortedModule,
ItemVisibility, LetStatement, NoirFunction, NoirStruct, PathKind, TraitImplItem, Type,
TypeImpl, UnresolvedTypeData, UnresolvedTypeExpression,
Type,
};

use regex::Regex;

use crate::{
Expand Down
8 changes: 5 additions & 3 deletions aztec_macros/src/transforms/storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::borrow::Borrow;

use noirc_errors::Span;
use noirc_frontend::ast::{
BlockExpression, Expression, ExpressionKind, FunctionDefinition, Ident, Literal, NoirFunction,
NoirStruct, PathKind, Pattern, StatementKind, TypeImpl, UnresolvedType, UnresolvedTypeData,
};
use noirc_frontend::{
graph::CrateId,
macros_api::{
Expand All @@ -10,9 +14,7 @@ use noirc_frontend::{
parse_program,
parser::SortedModule,
token::SecondaryAttribute,
BlockExpression, Expression, ExpressionKind, FunctionDefinition, Ident, Literal, NoirFunction,
NoirStruct, PathKind, Pattern, StatementKind, Type, TypeImpl, UnresolvedType,
UnresolvedTypeData,
Type,
};

use crate::{
Expand Down
11 changes: 6 additions & 5 deletions aztec_macros/src/utils/ast_utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use noirc_errors::{Span, Spanned};
use noirc_frontend::{
token::SecondaryAttribute, BinaryOpKind, CallExpression, CastExpression, Expression,
ExpressionKind, FunctionReturnType, Ident, IndexExpression, InfixExpression, Lambda,
LetStatement, MethodCallExpression, NoirTraitImpl, Path, Pattern, PrefixExpression, Statement,
StatementKind, TraitImplItem, UnaryOp, UnresolvedType, UnresolvedTypeData,
use noirc_frontend::ast::{
BinaryOpKind, CallExpression, CastExpression, Expression, ExpressionKind, FunctionReturnType,
Ident, IndexExpression, InfixExpression, Lambda, LetStatement, MethodCallExpression,
NoirTraitImpl, Path, Pattern, PrefixExpression, Statement, StatementKind, TraitImplItem,
UnaryOp, UnresolvedType, UnresolvedTypeData,
};
use noirc_frontend::token::SecondaryAttribute;

//
// Helper macros for creating noir ast nodes
Expand Down
9 changes: 5 additions & 4 deletions aztec_macros/src/utils/errors.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use noirc_errors::Span;
use noirc_frontend::{macros_api::MacroError, UnresolvedTypeData};
use noirc_frontend::ast;
use noirc_frontend::macros_api::MacroError;

use super::constants::MAX_CONTRACT_PRIVATE_FUNCTIONS;

#[derive(Debug, Clone)]
pub enum AztecMacroError {
AztecDepNotFound,
ContractHasTooManyPrivateFunctions { span: Span },
UnsupportedFunctionArgumentType { span: Span, typ: UnresolvedTypeData },
UnsupportedFunctionReturnType { span: Span, typ: UnresolvedTypeData },
UnsupportedStorageType { span: Option<Span>, typ: UnresolvedTypeData },
UnsupportedFunctionArgumentType { span: Span, typ: ast::UnresolvedTypeData },
UnsupportedFunctionReturnType { span: Span, typ: ast::UnresolvedTypeData },
UnsupportedStorageType { span: Option<Span>, typ: ast::UnresolvedTypeData },
CouldNotAssignStorageSlots { secondary_message: Option<String> },
CouldNotImplementComputeNoteHashAndNullifier { secondary_message: Option<String> },
CouldNotImplementNoteInterface { span: Option<Span>, secondary_message: Option<String> },
Expand Down
13 changes: 7 additions & 6 deletions aztec_macros/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use iter_extended::vecmap;
use noirc_errors::Location;
use noirc_frontend::ast;
use noirc_frontend::{
graph::CrateId,
hir::{
Expand All @@ -9,7 +10,7 @@ use noirc_frontend::{
},
macros_api::{FileId, HirContext, MacroError, ModuleDefId, StructId},
node_interner::{FuncId, TraitId},
ItemVisibility, LetStatement, NoirFunction, Shared, Signedness, StructType, Type,
Shared, StructType, Type,
};

use super::ast_utils::is_custom_attribute;
Expand Down Expand Up @@ -65,8 +66,8 @@ pub fn collect_traits(context: &HirContext) -> Vec<TraitId> {
/// Computes the aztec signature for a resolved type.
pub fn signature_of_type(typ: &Type) -> String {
match typ {
Type::Integer(Signedness::Signed, bit_size) => format!("i{}", bit_size),
Type::Integer(Signedness::Unsigned, bit_size) => format!("u{}", bit_size),
Type::Integer(ast::Signedness::Signed, bit_size) => format!("i{}", bit_size),
Type::Integer(ast::Signedness::Unsigned, bit_size) => format!("u{}", bit_size),
Type::FieldElement => "Field".to_owned(),
Type::Bool => "bool".to_owned(),
Type::Array(len, typ) => {
Expand Down Expand Up @@ -165,7 +166,7 @@ pub fn get_contract_module_data(
pub fn inject_fn(
crate_id: &CrateId,
context: &mut HirContext,
func: NoirFunction,
func: ast::NoirFunction,
location: Location,
module_id: LocalModuleId,
file_id: FileId,
Expand All @@ -179,7 +180,7 @@ pub fn inject_fn(
);

context.def_map_mut(crate_id).unwrap().modules_mut()[module_id.0]
.declare_function(func.name_ident().clone(), ItemVisibility::Public, func_id)
.declare_function(func.name_ident().clone(), ast::ItemVisibility::Public, func_id)
.map_err(|err| MacroError {
primary_message: format!("Failed to declare autogenerated {} function", func.name()),
secondary_message: Some(format!("Duplicate definition found {}", err.0)),
Expand Down Expand Up @@ -214,7 +215,7 @@ pub fn inject_fn(
pub fn inject_global(
crate_id: &CrateId,
context: &mut HirContext,
global: LetStatement,
global: ast::LetStatement,
module_id: LocalModuleId,
file_id: FileId,
) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_driver/src/abi_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::collections::BTreeMap;
use acvm::acir::native_types::Witness;
use iter_extended::{btree_map, vecmap};
use noirc_abi::{Abi, AbiParameter, AbiReturnType, AbiType, AbiValue};
use noirc_frontend::ast::Visibility;
use noirc_frontend::{
hir::Context,
hir_def::{expr::HirArrayLiteral, function::Param, stmt::HirPattern},
macros_api::{HirExpression, HirLiteral},
node_interner::{FuncId, NodeInterner},
Visibility,
};
use std::ops::Range;

Expand Down
5 changes: 2 additions & 3 deletions compiler/noirc_evaluator/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ use acvm::acir::{

use noirc_errors::debug_info::{DebugFunctions, DebugInfo, DebugTypes, DebugVariables};

use noirc_frontend::{
hir_def::function::FunctionSignature, monomorphization::ast::Program, Visibility,
};
use noirc_frontend::ast::Visibility;
use noirc_frontend::{hir_def::function::FunctionSignature, monomorphization::ast::Program};
use tracing::{span, Level};

use self::{acir_gen::GeneratedAcir, ssa_gen::Ssa};
Expand Down
10 changes: 5 additions & 5 deletions compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use acvm::{
use fxhash::FxHashMap as HashMap;
use im::Vector;
use iter_extended::{try_vecmap, vecmap};
use noirc_frontend::Distinctness;
use noirc_frontend::ast::Distinctness;

#[derive(Default)]
struct SharedContext {
Expand Down Expand Up @@ -2595,7 +2595,7 @@ mod test {
let ssa = builder.finish();

let (acir_functions, _) = ssa
.into_acir(&Brillig::default(), noirc_frontend::Distinctness::Distinct)
.into_acir(&Brillig::default(), noirc_frontend::ast::Distinctness::Distinct)
.expect("Should compile manually written SSA into ACIR");
// Expected result:
// main f0
Expand Down Expand Up @@ -2691,7 +2691,7 @@ mod test {
let ssa = builder.finish();

let (acir_functions, _) = ssa
.into_acir(&Brillig::default(), noirc_frontend::Distinctness::Distinct)
.into_acir(&Brillig::default(), noirc_frontend::ast::Distinctness::Distinct)
.expect("Should compile manually written SSA into ACIR");
// The expected result should look very similar to the abvoe test expect that the input witnesses of the `Call`
// opcodes will be different. The changes can discerned from the checks below.
Expand Down Expand Up @@ -2782,7 +2782,7 @@ mod test {
let ssa = builder.finish();

let (acir_functions, _) = ssa
.into_acir(&Brillig::default(), noirc_frontend::Distinctness::Distinct)
.into_acir(&Brillig::default(), noirc_frontend::ast::Distinctness::Distinct)
.expect("Should compile manually written SSA into ACIR");

assert_eq!(acir_functions.len(), 3, "Should have three ACIR functions");
Expand Down Expand Up @@ -2894,7 +2894,7 @@ mod test {
println!("{}", ssa);

let (acir_functions, brillig_functions) = ssa
.into_acir(&brillig, noirc_frontend::Distinctness::Distinct)
.into_acir(&brillig, noirc_frontend::ast::Distinctness::Distinct)
.expect("Should compile manually written SSA into ACIR");

assert_eq!(acir_functions.len(), 1, "Should only have a `main` ACIR function");
Expand Down
5 changes: 3 additions & 2 deletions compiler/noirc_evaluator/src/ssa/function_builder/data_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::rc::Rc;
use crate::ssa::ir::{types::Type, value::ValueId};
use acvm::FieldElement;
use fxhash::FxHashMap as HashMap;
use noirc_frontend::ast;
use noirc_frontend::hir_def::function::FunctionSignature;

use super::FunctionBuilder;
Expand Down Expand Up @@ -33,8 +34,8 @@ impl DataBusBuilder {

for param in &main_signature.0 {
let is_databus = match param.2 {
noirc_frontend::Visibility::Public | noirc_frontend::Visibility::Private => false,
noirc_frontend::Visibility::DataBus => true,
ast::Visibility::Public | ast::Visibility::Private => false,
ast::Visibility::DataBus => true,
};
let len = param.1.field_count() as usize;
params_is_databus.extend(vec![is_databus; len]);
Expand Down
20 changes: 10 additions & 10 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::sync::{Mutex, RwLock};
use acvm::FieldElement;
use iter_extended::vecmap;
use noirc_errors::Location;
use noirc_frontend::ast::{BinaryOpKind, Signedness};
use noirc_frontend::monomorphization::ast::{self, LocalId, Parameters};
use noirc_frontend::monomorphization::ast::{FuncId, Program};
use noirc_frontend::{BinaryOpKind, Signedness};

use crate::errors::RuntimeError;
use crate::ssa::function_builder::FunctionBuilder;
Expand Down Expand Up @@ -562,7 +562,7 @@ impl<'a> FunctionContext<'a> {
pub(super) fn insert_binary(
&mut self,
mut lhs: ValueId,
operator: noirc_frontend::BinaryOpKind,
operator: BinaryOpKind,
mut rhs: ValueId,
location: Location,
) -> Values {
Expand Down Expand Up @@ -625,7 +625,7 @@ impl<'a> FunctionContext<'a> {
fn insert_array_equality(
&mut self,
lhs: ValueId,
operator: noirc_frontend::BinaryOpKind,
operator: BinaryOpKind,
rhs: ValueId,
location: Location,
) -> Values {
Expand Down Expand Up @@ -1090,23 +1090,23 @@ impl<'a> FunctionContext<'a> {

/// True if the given operator cannot be encoded directly and needs
/// to be represented as !(some other operator)
fn operator_requires_not(op: noirc_frontend::BinaryOpKind) -> bool {
use noirc_frontend::BinaryOpKind::*;
fn operator_requires_not(op: BinaryOpKind) -> bool {
use BinaryOpKind::*;
matches!(op, NotEqual | LessEqual | GreaterEqual)
}

/// True if the given operator cannot be encoded directly and needs
/// to have its lhs and rhs swapped to be represented with another operator.
/// Example: (a > b) needs to be represented as (b < a)
fn operator_requires_swapped_operands(op: noirc_frontend::BinaryOpKind) -> bool {
use noirc_frontend::BinaryOpKind::*;
fn operator_requires_swapped_operands(op: BinaryOpKind) -> bool {
use BinaryOpKind::*;
matches!(op, Greater | LessEqual)
}

/// If the operation requires its result to be truncated because it is an integer, the maximum
/// number of bits that result may occupy is returned.
fn operator_result_max_bit_size_to_truncate(
op: noirc_frontend::BinaryOpKind,
op: BinaryOpKind,
lhs: ValueId,
rhs: ValueId,
dfg: &DataFlowGraph,
Expand All @@ -1123,7 +1123,7 @@ fn operator_result_max_bit_size_to_truncate(

let lhs_bit_size = get_bit_size(lhs_type)?;
let rhs_bit_size = get_bit_size(rhs_type)?;
use noirc_frontend::BinaryOpKind::*;
use BinaryOpKind::*;
match op {
Add => Some(std::cmp::max(lhs_bit_size, rhs_bit_size) + 1),
Subtract => Some(std::cmp::max(lhs_bit_size, rhs_bit_size) + 1),
Expand Down Expand Up @@ -1173,7 +1173,7 @@ fn operator_result_max_bit_size_to_truncate(
/// Take care when using this to insert a binary instruction: this requires
/// checking operator_requires_not and operator_requires_swapped_operands
/// to represent the full operation correctly.
fn convert_operator(op: noirc_frontend::BinaryOpKind) -> BinaryOp {
fn convert_operator(op: BinaryOpKind) -> BinaryOp {
match op {
BinaryOpKind::Add => BinaryOp::Add,
BinaryOpKind::Subtract => BinaryOp::Sub,
Expand Down
Loading

0 comments on commit 1969ce3

Please sign in to comment.