diff --git a/reid/src/codegen/allocator.rs b/reid/src/codegen/allocator.rs index c4cf301..5809de3 100644 --- a/reid/src/codegen/allocator.rs +++ b/reid/src/codegen/allocator.rs @@ -154,11 +154,7 @@ impl mir::Expression { allocated.extend(lhs.allocate(scope)); allocated.extend(rhs.allocate(scope)); } - mir::ExprKind::FunctionCall(FunctionCall { parameters, .. }) => { - for param in parameters { - allocated.extend(param.allocate(scope)); - } - } + mir::ExprKind::FunctionCall(fn_call) => allocated.extend(fn_call.allocate(&fn_call.name, scope)), mir::ExprKind::If(IfExpression(cond, then_ex, else_ex)) => { allocated.extend(cond.allocate(scope)); allocated.extend(then_ex.allocate(scope)); @@ -174,13 +170,34 @@ impl mir::Expression { mir::ExprKind::CastTo(expression, _) => { allocated.extend(expression.allocate(scope)); } - mir::ExprKind::AssociatedFunctionCall(_, FunctionCall { parameters, .. }) => { - for param in parameters { - allocated.extend(param.allocate(scope)); - } + mir::ExprKind::AssociatedFunctionCall(ty, fn_call) => { + allocated.extend(fn_call.allocate(&format!("{}::{}", ty, fn_call.name), scope)) } } allocated } } + +impl mir::FunctionCall { + fn allocate<'ctx, 'a>(&self, name: &String, scope: &mut AllocatorScope<'ctx, 'a>) -> Vec { + let mut allocated = Vec::new(); + + for param in &self.parameters { + allocated.extend(param.allocate(scope)); + } + + if self.return_type != TypeKind::Void { + let allocation = scope + .block + .build_named( + name, + reid_lib::Instr::Alloca(self.return_type.get_type(scope.type_values)), + ) + .unwrap(); + allocated.push(Allocation(name.clone(), self.return_type.clone(), allocation)); + } + + allocated + } +} diff --git a/reid/src/codegen/mod.rs b/reid/src/codegen/mod.rs index d59860d..fea0dce 100644 --- a/reid/src/codegen/mod.rs +++ b/reid/src/codegen/mod.rs @@ -1346,8 +1346,9 @@ fn codegen_function_call<'ctx, 'a>( let ptr = if ret_type_kind != TypeKind::Void { let ptr = scope - .block - .build_named(&call.name, Instr::Alloca(ret_type.clone())) + .allocator + .borrow_mut() + .allocate(&call_name, &call.return_type) .unwrap(); scope .block