diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 3ad69d7..4c87d28 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use crate::{ - ast::{self}, + ast::{self, FunctionCallExpression}, mir::{ self, CustomTypeKey, ModuleMap, NamedVariableRef, ReturnKind, SourceModuleId, StmtKind, StructField, StructType, WhileStatement, @@ -21,6 +21,7 @@ impl mir::Context { impl ast::Module { pub fn process(self, module_id: SourceModuleId) -> mir::Module { let mut imports = Vec::new(); + let mut associated_functions = Vec::new(); let mut functions = Vec::new(); let mut typedefs = Vec::new(); let mut binops = Vec::new(); @@ -31,44 +32,7 @@ impl ast::Module { Import(import) => { imports.push(mir::Import(import.0.clone(), import.1.as_meta(module_id))); } - FunctionDefinition(ast::FunctionDefinition(signature, is_pub, block, range)) => { - let mut params = Vec::new(); - match &signature.self_kind { - ast::SelfKind::Borrow(type_kind) => params.push(( - "self".to_owned(), - mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), false), - )), - ast::SelfKind::MutBorrow(type_kind) => params.push(( - "self".to_owned(), - mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), true), - )), - ast::SelfKind::None => {} - } - - params.extend( - signature - .params - .iter() - .cloned() - .map(|p| (p.0, p.1 .0.into_mir(module_id))), - ); - let def = mir::FunctionDefinition { - name: signature.name.clone(), - is_pub: *is_pub, - is_imported: false, - return_type: signature - .return_type - .clone() - .map(|r| r.0.into_mir(module_id)) - .unwrap_or(mir::TypeKind::Void), - parameters: params, - kind: mir::FunctionDefinitionKind::Local( - block.into_mir(module_id), - (*range).as_meta(module_id), - ), - }; - functions.push(def); - } + FunctionDefinition(function_def) => functions.push(function_def.into_mir(module_id)), ExternFunction(signature) => { let def = mir::FunctionDefinition { name: signature.name.clone(), @@ -135,7 +99,11 @@ impl ast::Module { exported: false, }); } - AssociatedFunction(_, function_definition) => todo!(), + AssociatedFunction(ty, function_definition) => { + for function_def in function_definition { + associated_functions.push((ty.0.into_mir(module_id), function_def.into_mir(module_id))); + } + } } } @@ -144,6 +112,7 @@ impl ast::Module { module_id: module_id, binop_defs: binops, imports, + associated_functions, functions, path: self.path.clone(), is_main: self.is_main, @@ -153,6 +122,45 @@ impl ast::Module { } } +impl ast::FunctionDefinition { + pub fn into_mir(&self, module_id: SourceModuleId) -> mir::FunctionDefinition { + let &ast::FunctionDefinition(signature, is_pub, block, range) = &self; + + let mut params = Vec::new(); + match &signature.self_kind { + ast::SelfKind::Borrow(type_kind) => params.push(( + "self".to_owned(), + mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), false), + )), + ast::SelfKind::MutBorrow(type_kind) => params.push(( + "self".to_owned(), + mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), true), + )), + ast::SelfKind::None => {} + } + + params.extend( + signature + .params + .iter() + .cloned() + .map(|p| (p.0, p.1 .0.into_mir(module_id))), + ); + mir::FunctionDefinition { + name: signature.name.clone(), + is_pub: *is_pub, + is_imported: false, + return_type: signature + .return_type + .clone() + .map(|r| r.0.into_mir(module_id)) + .unwrap_or(mir::TypeKind::Void), + parameters: params, + kind: mir::FunctionDefinitionKind::Local(block.into_mir(module_id), (range).as_meta(module_id)), + } + } +} + impl ast::Block { pub fn into_mir(&self, module_id: SourceModuleId) -> mir::Block { let mut mir_statements = Vec::new(); @@ -404,7 +412,15 @@ impl ast::Expression { .map(|e| e.process(module_id)) .collect(), ), - ast::ExpressionKind::AssociatedFunctionCall(_, function_call_expression) => todo!(), + ast::ExpressionKind::AssociatedFunctionCall(ty, fn_call_expr) => mir::ExprKind::AssociatedFunctionCall( + ty.0.into_mir(module_id), + mir::FunctionCall { + name: fn_call_expr.0.clone(), + return_type: mir::TypeKind::Vague(mir::VagueType::Unknown), + parameters: fn_call_expr.1.iter().map(|e| e.process(module_id)).collect(), + meta: fn_call_expr.2.as_meta(module_id), + }, + ), }; mir::Expression(kind, self.1.as_meta(module_id)) diff --git a/reid/src/codegen/allocator.rs b/reid/src/codegen/allocator.rs index 441064c..26dbfe9 100644 --- a/reid/src/codegen/allocator.rs +++ b/reid/src/codegen/allocator.rs @@ -5,10 +5,12 @@ use reid_lib::{ Block, }; -use crate::mir::{ - self, CustomTypeKey, FunctionCall, FunctionDefinitionKind, IfExpression, SourceModuleId, TypeKind, WhileStatement, +use mir::{ + CustomTypeKey, FunctionCall, FunctionDefinitionKind, IfExpression, SourceModuleId, TypeKind, WhileStatement, }; +use crate::mir; + #[derive(Debug)] pub struct Allocator { allocations: Vec, @@ -100,7 +102,7 @@ impl mir::Statement { let mut allocated = Vec::new(); match &self.0 { - crate::mir::StmtKind::Let(named_variable_ref, _, expression) => { + mir::StmtKind::Let(named_variable_ref, _, expression) => { allocated.extend(expression.allocate(scope)); let allocation = scope .block @@ -115,15 +117,15 @@ impl mir::Statement { allocation, )); } - crate::mir::StmtKind::Set(lhs, rhs) => { + mir::StmtKind::Set(lhs, rhs) => { allocated.extend(lhs.allocate(scope)); allocated.extend(rhs.allocate(scope)); } - crate::mir::StmtKind::Import(_) => {} - crate::mir::StmtKind::Expression(expression) => { + mir::StmtKind::Import(_) => {} + mir::StmtKind::Expression(expression) => { allocated.extend(expression.allocate(scope)); } - crate::mir::StmtKind::While(WhileStatement { condition, block, .. }) => { + mir::StmtKind::While(WhileStatement { condition, block, .. }) => { allocated.extend(condition.allocate(scope)); allocated.extend(block.allocate(scope)); } @@ -138,49 +140,50 @@ impl mir::Expression { let mut allocated = Vec::new(); match &self.0 { - crate::mir::ExprKind::Variable(_) => {} - crate::mir::ExprKind::Indexed(expr, _, idx) => { + mir::ExprKind::Variable(_) => {} + mir::ExprKind::Indexed(expr, _, idx) => { allocated.extend(expr.allocate(scope)); allocated.extend(idx.allocate(scope)); } - crate::mir::ExprKind::Accessed(expression, _, _) => { + mir::ExprKind::Accessed(expression, _, _) => { allocated.extend(expression.allocate(scope)); } - crate::mir::ExprKind::Array(expressions) => { + mir::ExprKind::Array(expressions) => { for expression in expressions { allocated.extend(expression.allocate(scope)); } } - crate::mir::ExprKind::Struct(_, items) => { + mir::ExprKind::Struct(_, items) => { for (_, expression) in items { allocated.extend(expression.allocate(scope)); } } - crate::mir::ExprKind::Literal(_) => {} - crate::mir::ExprKind::BinOp(_, lhs, rhs, _) => { + mir::ExprKind::Literal(_) => {} + mir::ExprKind::BinOp(_, lhs, rhs, _) => { allocated.extend(lhs.allocate(scope)); allocated.extend(rhs.allocate(scope)); } - crate::mir::ExprKind::FunctionCall(FunctionCall { parameters, .. }) => { + mir::ExprKind::FunctionCall(FunctionCall { parameters, .. }) => { for param in parameters { allocated.extend(param.allocate(scope)); } } - crate::mir::ExprKind::If(IfExpression(cond, then_ex, else_ex)) => { + mir::ExprKind::If(IfExpression(cond, then_ex, else_ex)) => { allocated.extend(cond.allocate(scope)); allocated.extend(then_ex.allocate(scope)); if let Some(else_ex) = else_ex.as_ref() { allocated.extend(else_ex.allocate(scope)); } } - crate::mir::ExprKind::Block(block) => { + mir::ExprKind::Block(block) => { allocated.extend(block.allocate(scope)); } - crate::mir::ExprKind::Borrow(_, _) => {} - crate::mir::ExprKind::Deref(_) => {} - crate::mir::ExprKind::CastTo(expression, _) => { + mir::ExprKind::Borrow(_, _) => {} + mir::ExprKind::Deref(_) => {} + mir::ExprKind::CastTo(expression, _) => { allocated.extend(expression.allocate(scope)); } + mir::ExprKind::AssociatedFunctionCall(type_kind, function_call) => todo!(), } allocated diff --git a/reid/src/codegen/mod.rs b/reid/src/codegen/mod.rs index d63d79c..8e3a31b 100644 --- a/reid/src/codegen/mod.rs +++ b/reid/src/codegen/mod.rs @@ -1217,6 +1217,7 @@ impl mir::Expression { } } } + mir::ExprKind::AssociatedFunctionCall(..) => todo!(), }; if let Some(value) = &value { value.instr().maybe_location(&mut scope.block, location); diff --git a/reid/src/mir/fmt.rs b/reid/src/mir/fmt.rs index 8e9b5f7..3b0fda2 100644 --- a/reid/src/mir/fmt.rs +++ b/reid/src/mir/fmt.rs @@ -272,6 +272,11 @@ impl Display for ExprKind { ExprKind::Borrow(var_ref, true) => write!(f, "&mut {}", var_ref), ExprKind::Deref(var_ref) => write!(f, "*{}", var_ref), ExprKind::CastTo(expression, type_kind) => write!(f, "{} as {}", expression, type_kind), + ExprKind::AssociatedFunctionCall(type_kind, function_call) => { + Display::fmt(type_kind, f)?; + write!(f, "::")?; + Display::fmt(function_call, f) + } } } } diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index d685dd9..36acda1 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -443,6 +443,7 @@ impl Expression { }, Err(_) => Ok((ReturnKind::Soft, type_kind.clone())), }, + AssociatedFunctionCall(type_kind, function_call) => todo!(), } } @@ -461,6 +462,7 @@ impl Expression { ExprKind::FunctionCall(_) => None, ExprKind::If(_) => None, ExprKind::CastTo(expression, _) => expression.backing_var(), + ExprKind::AssociatedFunctionCall(type_kind, function_call) => None, } } @@ -499,6 +501,7 @@ impl Expression { ExprKind::Borrow(_, _) => None, ExprKind::Deref(_) => None, ExprKind::CastTo(expression, _) => expression.num_value()?, + ExprKind::AssociatedFunctionCall(..) => None, }) } } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 96522eb..8b9c81a 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -258,6 +258,7 @@ pub enum ExprKind { Literal(Literal), BinOp(BinaryOperator, Box, Box, TypeKind), FunctionCall(FunctionCall), + AssociatedFunctionCall(TypeKind, FunctionCall), If(IfExpression), Block(Block), Borrow(NamedVariableRef, bool), @@ -399,6 +400,7 @@ pub struct Module { pub name: String, pub module_id: SourceModuleId, pub imports: Vec, + pub associated_functions: Vec<(TypeKind, FunctionDefinition)>, pub functions: Vec, pub typedefs: Vec, pub binop_defs: Vec, diff --git a/reid/src/mir/typecheck/typecheck.rs b/reid/src/mir/typecheck/typecheck.rs index 5b1304a..4162949 100644 --- a/reid/src/mir/typecheck/typecheck.rs +++ b/reid/src/mir/typecheck/typecheck.rs @@ -724,6 +724,7 @@ impl Expression { let expr = expression.typecheck(state, typerefs, HintKind::Default)?; expr.resolve_ref(typerefs).cast_into(type_kind) } + ExprKind::AssociatedFunctionCall(type_kind, function_call) => todo!(), } } } diff --git a/reid/src/mir/typecheck/typeinference.rs b/reid/src/mir/typecheck/typeinference.rs index 21ea795..bfc367d 100644 --- a/reid/src/mir/typecheck/typeinference.rs +++ b/reid/src/mir/typecheck/typeinference.rs @@ -566,6 +566,7 @@ impl Expression { expression.infer_types(state, type_refs)?; Ok(type_refs.from_type(type_kind).unwrap()) } + ExprKind::AssociatedFunctionCall(type_kind, function_call) => todo!(), } } }