Skip to content

Commit

Permalink
experimental support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunkar committed Mar 26, 2024
1 parent f6df9a9 commit 5eaf3a3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 11 deletions.
5 changes: 4 additions & 1 deletion noir/noir-repo/aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ mod utils;
use transforms::{
compute_note_hash_and_nullifier::inject_compute_note_hash_and_nullifier,
events::{generate_selector_impl, transform_events},
functions::{transform_function, transform_unconstrained, transform_vm_function},
functions::{
export_fn_abi, transform_function, transform_unconstrained, transform_vm_function,
},
note_interface::generate_note_interface_impl,
storage::{
assign_storage_slots, check_for_storage_definition, check_for_storage_implementation,
Expand Down Expand Up @@ -139,6 +141,7 @@ fn transform_module(module: &mut SortedModule) -> Result<bool, AztecMacroError>

// Apply transformations to the function based on collected attributes
if is_private || is_public {
export_fn_abi(&mut module.types, func)?;
transform_function(
if is_private { "Private" } else { "Public" },
func,
Expand Down
82 changes: 78 additions & 4 deletions noir/noir-repo/aztec_macros/src/transforms/functions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use convert_case::{Case, Casing};
use noirc_errors::Span;
use noirc_frontend::{
macros_api::FieldElement, BlockExpression, ConstrainKind, ConstrainStatement, Distinctness,
Expression, ExpressionKind, ForLoopStatement, ForRange, FunctionReturnType, Ident, Literal,
NoirFunction, Param, PathKind, Pattern, Signedness, Statement, StatementKind, UnresolvedType,
UnresolvedTypeData, Visibility,
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 crate::{
Expand Down Expand Up @@ -101,6 +101,80 @@ pub fn transform_function(
Ok(())
}

pub fn export_fn_abi(
types: &mut Vec<NoirStruct>,
func: &NoirFunction,
) -> Result<(), AztecMacroError> {
let mut parameters_struct_source: Option<&str> = None;

let struct_source = format!(
"
struct {}_parameters {{
{}
}}
",
func.name(),
func.parameters()
.iter()
.map(|param| {
let param_name = match param.pattern.clone() {
Pattern::Identifier(ident) => Ok(ident.0.contents),
_ => Err(AztecMacroError::AztecDepNotFound),
};

format!(
"{}: {}",
param_name.unwrap(),
param.typ.typ.to_string().replace("plain::", "")
)
})
.collect::<Vec<String>>()
.join(",\n"),
);

if func.parameters().len() > 0 {
parameters_struct_source = Some(&struct_source);
}

let mut program = String::new();

let parameters = if let Some(parameters_struct_source) = parameters_struct_source {
program.push_str(&parameters_struct_source);
format!("parameters: {}_parameters,\n", func.name())
} else {
"".to_string()
};

let return_type_str = func.return_type().typ.to_string().replace("plain::", "");
let return_type = if return_type_str != "()" {
format!("return_type: {},\n", return_type_str)
} else {
"".to_string()
};

let export_struct_source = format!(
"
#[abi(functions)]
struct {}_abi {{
{}{}
}}",
func.name(),
parameters,
return_type
);

program.push_str(&export_struct_source);

let (ast, errors) = parse_program(&program);
if !errors.is_empty() {
return Err(AztecMacroError::AztecDepNotFound);
}

let sorted_ast = ast.into_sorted();
types.extend(sorted_ast.types);
Ok(())
}

/// Transform a function to work with AVM bytecode
pub fn transform_vm_function(
func: &mut NoirFunction,
Expand Down
6 changes: 0 additions & 6 deletions noir/noir-repo/aztec_macros/src/utils/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub enum AztecMacroError {
UnsupportedStorageType { span: Option<Span>, typ: UnresolvedTypeData },
CouldNotAssignStorageSlots { secondary_message: Option<String> },
CouldNotImplementNoteInterface { span: Option<Span>, secondary_message: Option<String> },
MultipleStorageDefinitions { span: Option<Span> },
EventError { span: Span, message: String },
UnsupportedAttributes { span: Span, secondary_message: Option<String> },
}
Expand Down Expand Up @@ -49,11 +48,6 @@ impl From<AztecMacroError> for MacroError {
secondary_message,
span
},
AztecMacroError::MultipleStorageDefinitions { span } => MacroError {
primary_message: "Only one struct can be tagged as #[aztec(storage)]".to_string(),
secondary_message: None,
span,
},
AztecMacroError::EventError { span, message } => MacroError {
primary_message: message,
secondary_message: None,
Expand Down

0 comments on commit 5eaf3a3

Please sign in to comment.