Skip to content

Commit

Permalink
fix: replace type_of usage and remove it, as hinted in review
Browse files Browse the repository at this point in the history
  • Loading branch information
alehander92 committed Jul 28, 2023
1 parent 0f1e8f3 commit fab657a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 63 deletions.
54 changes: 0 additions & 54 deletions crates/noirc_frontend/src/monomorphization/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,60 +229,6 @@ impl Type {
}
}

impl Expression {
pub fn type_of(&self) -> Type {
match self {
Expression::Ident(ident) => ident.typ.clone(),
Expression::Literal(lit) => match lit {
Literal::Integer(_, typ) => typ.clone(),
Literal::Bool(_) => Type::Bool,
Literal::Str(str) => Type::String(str.len() as u64),
Literal::Array(array) => {
Type::Array(array.contents.len() as u64, Box::new(array.element_type.clone()))
}
},
Expression::Block(stmts) => (stmts.last().unwrap()).type_of(),
Expression::Unary(unary) => unary.result_type.clone(),
Expression::Binary(_binary) => {
unreachable!("TODO: How do we get the type of a Binary op")
}
Expression::Index(index) => index.element_type.clone(),
Expression::Cast(cast) => cast.r#type.clone(),
Expression::For(_for_expr) => Type::Unit,
Expression::If(if_expr) => if_expr.typ.clone(),
Expression::Tuple(elements) => {
Type::Tuple(elements.iter().map(|e| e.type_of()).collect())
}
Expression::ExtractTupleField(tuple, index) => match tuple.as_ref() {
Expression::Tuple(fields) => fields[*index].type_of(),
_ => unreachable!("ICE: Tuple field access on non-tuple type"),
},
Expression::Call(call) => call.return_type.clone(),
Expression::Let(let_stmt) => let_stmt.expression.as_ref().type_of(),
Expression::Constrain(constraint, _) => constraint.as_ref().type_of(),
Expression::Assign(assign) => assign.lvalue.type_of(),
Expression::Semi(_expr) => Type::Unit,
}
}
}

impl LValue {
pub fn type_of(&self) -> Type {
match self {
LValue::Ident(ident) => ident.typ.clone(),
LValue::Index { element_type, .. } => element_type.clone(),
LValue::MemberAccess { object, field_index } => {
let tuple_type = object.as_ref().type_of();
match tuple_type {
Type::Tuple(fields) => fields[*field_index].clone(),
_ => unreachable!("ICE: Member access on non-tuple type"),
}
}
LValue::Dereference { element_type, .. } => element_type.clone(),
}
}
}

#[derive(Debug, Clone)]
pub struct Program {
pub functions: Vec<Function>,
Expand Down
27 changes: 18 additions & 9 deletions crates/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl<'interner> Monomorphizer<'interner> {
}
HirExpression::Constructor(constructor) => self.constructor(constructor, expr),

HirExpression::Lambda(lambda) => self.lambda(lambda),
HirExpression::Lambda(lambda) => self.lambda(lambda, expr),

HirExpression::MethodCall(_) => {
unreachable!("Encountered HirExpression::MethodCall during monomorphization")
Expand Down Expand Up @@ -674,8 +674,8 @@ impl<'interner> Monomorphizer<'interner> {
}
}

fn is_function_closure(&self, func: &ast::Expression) -> bool {
let t = func.type_of();
fn is_function_closure(&self, raw_func_id: node_interner::ExprId) -> bool {
let t = Self::convert_type(&self.interner.id_type(raw_func_id));
if self.is_function_closure_type(&t) {
true
} else if let ast::Type::Tuple(elements) = t {
Expand Down Expand Up @@ -723,12 +723,12 @@ impl<'interner> Monomorphizer<'interner> {

let mut block_expressions = vec![];

let is_closure = self.is_function_closure(&original_func);
let is_closure = self.is_function_closure(call.func);
if is_closure {
let extracted_func: ast::Expression;
let hir_call_func = self.interner.expression(&call.func);
if let HirExpression::Lambda(l) = hir_call_func {
let (setup, closure_variable) = self.lambda_with_setup(l);
let (setup, closure_variable) = self.lambda_with_setup(l, call.func);
block_expressions.push(setup);
extracted_func = closure_variable;
} else {
Expand Down Expand Up @@ -942,7 +942,11 @@ impl<'interner> Monomorphizer<'interner> {
}
}

fn lambda_with_setup(&mut self, lambda: HirLambda) -> (ast::Expression, ast::Expression) {
fn lambda_with_setup(
&mut self,
lambda: HirLambda,
expr: node_interner::ExprId,
) -> (ast::Expression, ast::Expression) {
// returns (<closure setup>, <closure variable>)
// which can be used directly in callsites or transformed
// directly to a single `Expression`
Expand Down Expand Up @@ -991,7 +995,12 @@ impl<'interner> Monomorphizer<'interner> {
}
}
}));
let env_typ = env_tuple.type_of();
let expr_type = self.interner.id_type(expr);
let env_typ = if let Type::Function(_, _, function_env_type) = expr_type {
Self::convert_type(&function_env_type)
} else {
unreachable!("expected a Function type for a Lambda node")
};

let env_let_stmt = ast::Expression::Let(ast::Let {
id: env_local_id,
Expand Down Expand Up @@ -1061,8 +1070,8 @@ impl<'interner> Monomorphizer<'interner> {
// ast::Expression::Block(vec![block_let_stmt, closure_ident])
}

fn lambda(&mut self, lambda: HirLambda) -> ast::Expression {
let (setup, closure_variable) = self.lambda_with_setup(lambda);
fn lambda(&mut self, lambda: HirLambda, expr: node_interner::ExprId) -> ast::Expression {
let (setup, closure_variable) = self.lambda_with_setup(lambda, expr);
ast::Expression::Block(vec![setup, closure_variable])
}

Expand Down

0 comments on commit fab657a

Please sign in to comment.