Skip to content

Commit

Permalink
Extract a has_named_attribute helper
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Sep 5, 2024
1 parent 991e683 commit 9eb5322
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 62 deletions.
75 changes: 13 additions & 62 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use builtin_helpers::{
check_one_argument, check_three_arguments, check_two_arguments, get_expr, get_field,
get_format_string, get_function_def, get_module, get_quoted, get_slice, get_struct,
get_trait_constraint, get_trait_def, get_trait_impl, get_tuple, get_type, get_typed_expr,
get_u32, get_unresolved_type, hir_pattern_to_tokens, mutate_func_meta_type, parse,
replace_func_meta_parameters, replace_func_meta_return_type,
get_u32, get_unresolved_type, has_named_attribute, hir_pattern_to_tokens,
mutate_func_meta_type, parse, replace_func_meta_parameters, replace_func_meta_return_type,
};
use chumsky::{chain::Chain, prelude::choice, Parser};
use im::Vector;
Expand All @@ -25,7 +25,6 @@ use crate::{
FunctionReturnType, IntegerBitSize, LValue, Literal, Statement, StatementKind, UnaryOp,
UnresolvedType, UnresolvedTypeData, Visibility,
},
elaborator::Elaborator,
hir::comptime::{
errors::IResult,
value::{ExprValue, TypedExpr},
Expand Down Expand Up @@ -358,33 +357,15 @@ fn struct_def_has_named_attribute(
) -> IResult<Value> {
let (self_argument, name) = check_two_arguments(arguments, location)?;
let struct_id = get_struct(self_argument)?;
let name = get_quoted(name)?;
let attributes = interner.struct_attributes(&struct_id);
let attributes: Vec<_> =
attributes.iter().filter_map(|attribute| attribute.as_custom()).collect();

if attributes.is_empty() {
return Ok(Value::Bool(false));
};

let name = get_quoted(name)?;
let name = name.iter().map(|token| token.to_string()).collect::<Vec<_>>().join("");

for attribute in attributes {
let parse_result = Elaborator::parse_attribute(&attribute.contents, location);
let Ok(Some((function, _arguments))) = parse_result else {
continue;
};

let ExpressionKind::Variable(path) = function.kind else {
continue;
};

if path.last_name() == name {
return Ok(Value::Bool(true));
}
}
let attributes = interner.struct_attributes(&struct_id);
let attributes = attributes.iter().filter_map(|attribute| attribute.as_custom());
let attributes = attributes.map(|attribute| &attribute.contents);

Ok(Value::Bool(false))
Ok(Value::Bool(has_named_attribute(&name, attributes, location)))
}

/// fn fields(self) -> [(Quoted, Type)]
Expand Down Expand Up @@ -1827,31 +1808,15 @@ fn function_def_has_named_attribute(
) -> IResult<Value> {
let (self_argument, name) = check_two_arguments(arguments, location)?;
let func_id = get_function_def(self_argument)?;
let name = get_quoted(name)?;
let func_meta = interner.function_meta(&func_id);
let attributes = &func_meta.custom_attributes;
if attributes.is_empty() {
return Ok(Value::Bool(false));
};

let name = get_quoted(name)?;
let name = name.iter().map(|token| token.to_string()).collect::<Vec<_>>().join("");

for attribute in attributes {
let parse_result = Elaborator::parse_attribute(&attribute.contents, location);
let Ok(Some((function, _arguments))) = parse_result else {
continue;
};

let ExpressionKind::Variable(path) = function.kind else {
continue;
};

if path.last_name() == name {
return Ok(Value::Bool(true));
}
}
let attributes = &func_meta.custom_attributes;
let attributes = attributes.iter().map(|attribute| &attribute.contents);

Ok(Value::Bool(false))
Ok(Value::Bool(has_named_attribute(&name, attributes, location)))
}

// fn name(self) -> Quoted
Expand Down Expand Up @@ -2053,27 +2018,13 @@ fn module_has_named_attribute(
let (self_argument, name) = check_two_arguments(arguments, location)?;
let module_id = get_module(self_argument)?;
let module_data = interpreter.elaborator.get_module(module_id);
let name = get_quoted(name)?;

let name = get_quoted(name)?;
let name = name.iter().map(|token| token.to_string()).collect::<Vec<_>>().join("");

let attributes = module_data.outer_attributes.iter().chain(&module_data.inner_attributes);
for attribute in attributes {
let parse_result = Elaborator::parse_attribute(attribute, location);
let Ok(Some((function, _arguments))) = parse_result else {
continue;
};

let ExpressionKind::Variable(path) = function.kind else {
continue;
};

if path.last_name() == name {
return Ok(Value::Bool(true));
}
}

Ok(Value::Bool(false))
Ok(Value::Bool(has_named_attribute(&name, attributes, location)))
}

// fn is_contract(self) -> bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
BlockExpression, ExpressionKind, IntegerBitSize, LValue, Signedness, StatementKind,
UnresolvedTypeData,
},
elaborator::Elaborator,
hir::{
comptime::{
errors::IResult,
Expand Down Expand Up @@ -437,3 +438,26 @@ pub(super) fn block_expression_to_value(block_expr: BlockExpression) -> Value {

Value::Slice(statements, typ)
}

pub(super) fn has_named_attribute<'a>(
name: &'a str,
attributes: impl Iterator<Item = &'a String>,
location: Location,
) -> bool {
for attribute in attributes {
let parse_result = Elaborator::parse_attribute(attribute, location);
let Ok(Some((function, _arguments))) = parse_result else {
continue;
};

let ExpressionKind::Variable(path) = function.kind else {
continue;
};

if path.last_name() == name {
return true;
}
}

false
}

0 comments on commit 9eb5322

Please sign in to comment.