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

feat: add FunctionDef::body #5825

Merged
merged 55 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
4267688
Add `FunctionDef::body`
asterite Aug 26, 2024
dae279a
WIP: add aztec_macro_add_context example
asterite Aug 26, 2024
1c50a48
Be able to turn ExpressionKind into tokens
asterite Aug 26, 2024
9440f9d
Be able to turn StatementKind into tokens
asterite Aug 26, 2024
6b4404a
`Quoted::as_expr` now works with statements and lvalue
asterite Aug 26, 2024
c7b13fc
Map call arguments
asterite Aug 26, 2024
4fd315a
Add a TODO
asterite Aug 26, 2024
ed93bac
clippy
asterite Aug 26, 2024
af11fc6
Add a TODO
asterite Aug 26, 2024
fa9d119
Move test program
asterite Aug 26, 2024
8d63ff2
Revert "Move test program"
asterite Aug 26, 2024
16d3b9e
Renames
asterite Aug 27, 2024
83248f9
Let `FunctionDef::body` return a single expression
asterite Aug 27, 2024
0a05fc8
Use `map`
asterite Aug 27, 2024
f5dc0f5
Add `Expr::quoted`
asterite Aug 27, 2024
269075c
Put semicolons in block expression
asterite Aug 27, 2024
93954e1
Rename test program
asterite Aug 27, 2024
0403574
Rename `aztec` to `inject_context`
asterite Aug 27, 2024
85d7fb3
Make `_context` the first argument
asterite Aug 27, 2024
32d411e
Let `println` work well with resolved and interned values
asterite Aug 27, 2024
4491a5e
Intern LValue too
asterite Aug 27, 2024
49613a3
Unwrap interned values in `expr_as`
asterite Aug 27, 2024
4158665
`Expr::map` for method call
asterite Aug 27, 2024
d133943
Add a test to map an integer
asterite Aug 27, 2024
06031f3
`Expr::map` for binary op
asterite Aug 27, 2024
c01b1bc
Intern UnresolvedTypeData
asterite Aug 27, 2024
618eac5
`Expr::map` for comptime
asterite Aug 27, 2024
bd37b78
Expr::map for function call
asterite Aug 27, 2024
986ff46
Expr::map for if
asterite Aug 27, 2024
e911da0
Expr::map for index
asterite Aug 27, 2024
a0df942
Expr::map for member access
asterite Aug 27, 2024
5e5c4b6
Quoted is a type
asterite Aug 27, 2024
b136e05
Expr::map for repeated array element
asterite Aug 27, 2024
f0b8471
Expr::map for repeated element slice
asterite Aug 27, 2024
62f3d36
Expr::map for slice
asterite Aug 27, 2024
850eaa0
Expr::map for tuple
asterite Aug 27, 2024
faad8c8
Expr::map for unary op
asterite Aug 27, 2024
86e3791
Expr::map for unsafe
asterite Aug 27, 2024
1fbe2b6
Add docs for Expr functions
asterite Aug 27, 2024
bb65ee3
Add docs for operators
asterite Aug 27, 2024
4eba2f8
Fix docs
asterite Aug 27, 2024
b2420dd
Merge branch 'master' into ab/aztec-macro-add-context
asterite Aug 27, 2024
16e3f1d
Remove TODO
asterite Aug 28, 2024
3cdbeda
map -> mutate
asterite Aug 28, 2024
61b8d07
map -> mutate in test program
asterite Aug 28, 2024
dff525a
Put parenthesis around operands
asterite Aug 28, 2024
36b825e
Let `FunctionDef::set_body` take an `Expr`
asterite Aug 28, 2024
e58b761
clippy
asterite Aug 28, 2024
e9b98af
Use zeroed for unreachable branches
asterite Aug 28, 2024
222e681
Use `crate::` inside std
asterite Aug 28, 2024
d27c93d
Let `Expr::mutate` always recur
asterite Aug 28, 2024
882f647
Update docs
asterite Aug 28, 2024
8781b14
Use `map`
asterite Aug 28, 2024
e99b17a
Have to call `f(self)` for expressions that don't have sub-expressions
asterite Aug 28, 2024
59cc511
resolve -> remove_interned_in
asterite Aug 28, 2024
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
12 changes: 10 additions & 2 deletions aztec_macros/src/utils/parse_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@
StatementKind::For(for_loop_statement) => empty_for_loop_statement(for_loop_statement),
StatementKind::Comptime(statement) => empty_statement(statement),
StatementKind::Semi(expression) => empty_expression(expression),
StatementKind::Break | StatementKind::Continue | StatementKind::Error => (),
StatementKind::Break
| StatementKind::Continue
| StatementKind::Interned(_)
| StatementKind::Error => (),
}
}

Expand Down Expand Up @@ -271,12 +274,15 @@
ExpressionKind::Unsafe(block_expression, _span) => {
empty_block_expression(block_expression);
}
ExpressionKind::Quote(..) | ExpressionKind::Resolved(_) | ExpressionKind::Error => (),
ExpressionKind::AsTraitPath(path) => {
empty_unresolved_type(&mut path.typ);
empty_path(&mut path.trait_path);
empty_ident(&mut path.impl_item);
}
ExpressionKind::Quote(..)
| ExpressionKind::Resolved(_)
| ExpressionKind::Interned(_)
| ExpressionKind::Error => (),
}
}

Expand Down Expand Up @@ -353,6 +359,7 @@
| UnresolvedTypeData::Unit
| UnresolvedTypeData::Quoted(_)
| UnresolvedTypeData::Resolved(_)
| UnresolvedTypeData::Interned(_)
| UnresolvedTypeData::Unspecified
| UnresolvedTypeData::Error => (),
}
Expand Down Expand Up @@ -397,9 +404,9 @@
}

fn empty_unresolved_trait_constraints(
unresolved_trait_constriants: &mut [UnresolvedTraitConstraint],

Check warning on line 407 in aztec_macros/src/utils/parse_utils.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (constriants)
) {
for trait_constraint in unresolved_trait_constriants.iter_mut() {

Check warning on line 409 in aztec_macros/src/utils/parse_utils.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (constriants)
empty_unresolved_trait_constraint(trait_constraint);
}
}
Expand Down Expand Up @@ -531,6 +538,7 @@
empty_expression(index);
}
LValue::Dereference(lvalue, _) => empty_lvalue(lvalue),
LValue::Interned(..) => (),
}
}

Expand Down
8 changes: 7 additions & 1 deletion compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::ast::{
};
use crate::hir::def_collector::errors::DefCollectorErrorKind;
use crate::macros_api::StructId;
use crate::node_interner::{ExprId, QuotedTypeId};
use crate::node_interner::{ExprId, InternedExpressionKind, QuotedTypeId};
use crate::token::{Attributes, FunctionAttribute, Token, Tokens};
use crate::{Kind, Type};
use acvm::{acir::AcirField, FieldElement};
Expand Down Expand Up @@ -43,6 +43,11 @@ pub enum ExpressionKind {
// code. It is used to translate function values back into the AST while
// guaranteeing they have the same instantiated type and definition id without resolving again.
Resolved(ExprId),

// This is an interned ExpressionKind during comptime code.
// The actual ExpressionKind can be retrieved with a NodeInterner.
Interned(InternedExpressionKind),

Error,
}

Expand Down Expand Up @@ -603,6 +608,7 @@ impl Display for ExpressionKind {
Unsafe(block, _) => write!(f, "unsafe {block}"),
Error => write!(f, "Error"),
Resolved(_) => write!(f, "?Resolved"),
Interned(_) => write!(f, "?Interned"),
Unquote(expr) => write!(f, "$({expr})"),
Quote(tokens) => {
let tokens = vecmap(&tokens.0, ToString::to_string);
Expand Down
7 changes: 6 additions & 1 deletion compiler/noirc_frontend/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
pub use type_alias::*;

use crate::{
node_interner::QuotedTypeId,
node_interner::{InternedUnresolvedTypeData, QuotedTypeId},
parser::{ParserError, ParserErrorReason},
token::IntType,
BinaryTypeOperator,
Expand Down Expand Up @@ -141,6 +141,10 @@
/// as a result of being spliced into a macro's token stream input.
Resolved(QuotedTypeId),

// This is an interned UnresolvedTypeData during comptime code.
// The actual UnresolvedTypeData can be retrieved with a NodeInterner.
Interned(InternedUnresolvedTypeData),

Unspecified, // This is for when the user declares a variable without specifying it's type
Error,
}
Expand Down Expand Up @@ -297,6 +301,7 @@
Unspecified => write!(f, "unspecified"),
Parenthesized(typ) => write!(f, "({typ})"),
Resolved(_) => write!(f, "(resolved type)"),
Interned(_) => write!(f, "?Interned"),
AsTraitPath(path) => write!(f, "{path}"),
}
}
Expand Down Expand Up @@ -480,7 +485,7 @@
Self::Public => write!(f, "pub"),
Self::Private => write!(f, "priv"),
Self::CallData(id) => write!(f, "calldata{id}"),
Self::ReturnData => write!(f, "returndata"),

Check warning on line 488 in compiler/noirc_frontend/src/ast/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (returndata)
}
}
}
48 changes: 47 additions & 1 deletion compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::{
use crate::elaborator::types::SELF_TYPE_NAME;
use crate::lexer::token::SpannedToken;
use crate::macros_api::{SecondaryAttribute, UnresolvedTypeData};
use crate::node_interner::{InternedExpressionKind, InternedStatementKind};
use crate::parser::{ParserError, ParserErrorReason};
use crate::token::Token;

Expand Down Expand Up @@ -45,6 +46,9 @@ pub enum StatementKind {
Comptime(Box<Statement>),
// This is an expression with a trailing semi-colon
Semi(Expression),
// This is an interned StatementKind during comptime code.
// The actual StatementKind can be retrieved with a NodeInterner.
Interned(InternedStatementKind),
// This statement is the result of a recovered parse error.
// To avoid issuing multiple errors in later steps, it should
// be skipped in any future analysis if possible.
Expand Down Expand Up @@ -97,6 +101,9 @@ impl StatementKind {
// A semicolon on a for loop is optional and does nothing
StatementKind::For(_) => self,

// No semicolon needed for a resolved statement
StatementKind::Interned(_) => self,
asterite marked this conversation as resolved.
Show resolved Hide resolved

StatementKind::Expression(expr) => {
match (&expr.kind, semi, last_statement_in_block) {
// Semicolons are optional for these expressions
Expand Down Expand Up @@ -534,6 +541,7 @@ pub enum LValue {
MemberAccess { object: Box<LValue>, field_name: Ident, span: Span },
Index { array: Box<LValue>, index: Expression, span: Span },
Dereference(Box<LValue>, Span),
Interned(InternedExpressionKind, Span),
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -591,7 +599,7 @@ impl Recoverable for Pattern {
}

impl LValue {
fn as_expression(&self) -> Expression {
pub fn as_expression(&self) -> Expression {
let kind = match self {
LValue::Ident(ident) => ExpressionKind::Variable(Path::from_ident(ident.clone())),
LValue::MemberAccess { object, field_name, span: _ } => {
Expand All @@ -612,17 +620,53 @@ impl LValue {
rhs: lvalue.as_expression(),
}))
}
LValue::Interned(id, _) => ExpressionKind::Interned(*id),
};
let span = self.span();
Expression::new(kind, span)
}

pub fn from_expression(expr: Expression) -> LValue {
LValue::from_expression_kind(expr.kind, expr.span)
}

pub fn from_expression_kind(expr: ExpressionKind, span: Span) -> LValue {
match expr {
ExpressionKind::Variable(path) => LValue::Ident(path.as_ident().unwrap().clone()),
ExpressionKind::MemberAccess(member_access) => LValue::MemberAccess {
object: Box::new(LValue::from_expression(member_access.lhs)),
field_name: member_access.rhs,
span,
},
ExpressionKind::Index(index) => LValue::Index {
array: Box::new(LValue::from_expression(index.collection)),
index: index.index,
span,
},
ExpressionKind::Prefix(prefix) => {
if matches!(
prefix.operator,
crate::ast::UnaryOp::Dereference { implicitly_added: false }
) {
LValue::Dereference(Box::new(LValue::from_expression(prefix.rhs)), span)
} else {
panic!("Called LValue::from_expression with an invalid prefix operator")
}
}
ExpressionKind::Interned(id) => LValue::Interned(id, span),
_ => {
panic!("Called LValue::from_expression with an invalid expression")
jfecher marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

pub fn span(&self) -> Span {
match self {
LValue::Ident(ident) => ident.span(),
LValue::MemberAccess { span, .. }
| LValue::Index { span, .. }
| LValue::Dereference(_, span) => *span,
LValue::Interned(_, span) => *span,
}
}
}
Expand Down Expand Up @@ -777,6 +821,7 @@ impl Display for StatementKind {
StatementKind::Continue => write!(f, "continue"),
StatementKind::Comptime(statement) => write!(f, "comptime {}", statement.kind),
StatementKind::Semi(semi) => write!(f, "{semi};"),
StatementKind::Interned(_) => write!(f, "(resolved);"),
StatementKind::Error => write!(f, "Error"),
}
}
Expand Down Expand Up @@ -809,6 +854,7 @@ impl Display for LValue {
}
LValue::Index { array, index, span: _ } => write!(f, "{array}[{index}]"),
LValue::Dereference(lvalue, _span) => write!(f, "*{lvalue}"),
LValue::Interned(_, _) => write!(f, "?Interned"),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/noirc_frontend/src/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
}
ast::LValue::Dereference(_lv, span) => {
// TODO: this is a dummy statement for now, but we should
// somehow track the derefence and update the pointed to

Check warning on line 294 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (derefence)
// variable
ast::Statement {
kind: ast::StatementKind::Expression(uint_expr(0, *span)),
Expand Down Expand Up @@ -322,6 +322,9 @@
ast::LValue::Dereference(_ref, _span) => {
unimplemented![]
}
ast::LValue::Interned(..) => {
unimplemented![]
}
}
}
build_assign_member_stmt(
Expand Down Expand Up @@ -674,9 +677,9 @@
ast::Pattern::Tuple(patterns, _) => {
stack.extend(patterns.iter().map(|pattern| (pattern, false)));
}
ast::Pattern::Struct(_, pids, _) => {

Check warning on line 680 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (pids)
stack.extend(pids.iter().map(|(_, pattern)| (pattern, is_mut)));

Check warning on line 681 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (pids)
vars.extend(pids.iter().map(|(id, _)| (id.clone(), false)));

Check warning on line 682 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (pids)
}
}
}
Expand All @@ -686,7 +689,7 @@
fn pattern_to_string(pattern: &ast::Pattern) -> String {
match pattern {
ast::Pattern::Identifier(id) => id.0.contents.clone(),
ast::Pattern::Mutable(mpat, _, _) => format!("mut {}", pattern_to_string(mpat.as_ref())),

Check warning on line 692 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (mpat)

Check warning on line 692 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (mpat)
ast::Pattern::Tuple(elements, _) => format!(
"({})",
elements.iter().map(pattern_to_string).collect::<Vec<String>>().join(", ")
Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ impl<'context> Elaborator<'context> {
self.elaborate_unsafe_block(block_expression)
}
ExpressionKind::Resolved(id) => return (id, self.interner.id_type(id)),
ExpressionKind::Interned(id) => {
let expr_kind = self.interner.get_expression_kind(id);
let expr = Expression::new(expr_kind.clone(), expr.span);
return self.elaborate_expression(expr);
}
ExpressionKind::Error => (HirExpression::Error, Type::Error),
ExpressionKind::Unquote(_) => {
self.push_err(ResolverError::UnquoteUsedOutsideQuote { span: expr.span });
Expand Down
9 changes: 9 additions & 0 deletions compiler/noirc_frontend/src/elaborator/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ impl<'context> Elaborator<'context> {
let (expr, _typ) = self.elaborate_expression(expr);
(HirStatement::Semi(expr), Type::Unit)
}
StatementKind::Interned(id) => {
let kind = self.interner.get_statement_kind(id);
let statement = Statement { kind: kind.clone(), span: statement.span };
self.elaborate_statement_value(statement)
}
StatementKind::Error => (HirStatement::Error, Type::Error),
}
}
Expand Down Expand Up @@ -357,6 +362,10 @@ impl<'context> Elaborator<'context> {
let lvalue = HirLValue::Dereference { lvalue, element_type, location };
(lvalue, typ, true)
}
LValue::Interned(id, span) => {
let lvalue = self.interner.get_lvalue(id, span).clone();
self.elaborate_lvalue(lvalue, assign_span)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ impl<'context> Elaborator<'context> {
Parenthesized(typ) => self.resolve_type_inner(*typ, kind),
Resolved(id) => self.interner.get_quoted_type(id).clone(),
AsTraitPath(path) => self.resolve_as_trait_path(*path),
Interned(id) => {
let typ = self.interner.get_unresolved_type_data(id).clone();
return self.resolve_type_inner(UnresolvedType { typ, span }, kind);
}
};

let location = Location::new(named_path_span.unwrap_or(typ.span), self.file);
Expand Down
Loading
Loading