From 12dc457b995016345af2e5e494ddce3a1b4209d4 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 7 Jul 2025 18:32:37 +0300 Subject: [PATCH] Add mandatory return type for MIR Function --- reid/src/ast/parse.rs | 2 +- reid/src/ast/process.rs | 6 +++++- reid/src/codegen.rs | 7 +++---- reid/src/mir/mod.rs | 4 ++-- reid/src/mir/types.rs | 16 ++++++++-------- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index af462e6..df057f1 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -1,7 +1,7 @@ use crate::ast::*; use crate::{ lexer::Token, - token_stream::{Error, TokenRange, TokenStream}, + token_stream::{Error, TokenStream}, }; pub trait Parse diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 2103ca5..bf6ae42 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use crate::{ - ast, + ast::{self, TypeKind}, mir::{self, StmtKind, VariableReference}, }; @@ -176,6 +176,10 @@ impl ast::Module { let def = mir::FunctionDefinition { name: signature.name.clone(), + return_type: signature + .return_type + .map(|r| r.0.into()) + .unwrap_or(mir::TypeKind::Void), parameters: signature .args .iter() diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 7c346e5..a7572f2 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -20,7 +20,6 @@ impl mir::Module { let mut functions = HashMap::new(); for function in &self.functions { - let ret_type = function.return_type().unwrap().get_type(); let param_types: Vec = function .parameters .iter() @@ -29,9 +28,9 @@ impl mir::Module { let func = match &function.kind { mir::FunctionDefinitionKind::Local(_, _) => { - module.function(&function.name, ret_type, param_types) + module.function(&function.name, function.return_type.get_type(), param_types) } - mir::FunctionDefinitionKind::Extern(_) => todo!(), + mir::FunctionDefinitionKind::Extern => todo!(), }; functions.insert(function.name.clone(), func); } @@ -62,7 +61,7 @@ impl mir::Module { scope.block.terminate(TerminatorKind::Ret(ret)).unwrap(); } } - mir::FunctionDefinitionKind::Extern(_) => {} + mir::FunctionDefinitionKind::Extern => {} } } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 96573dd..4b11592 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -114,6 +114,7 @@ pub struct FunctionCall { #[derive(Debug)] pub struct FunctionDefinition { pub name: String, + pub return_type: TypeKind, pub parameters: Vec<(String, TypeKind)>, pub kind: FunctionDefinitionKind, } @@ -122,8 +123,7 @@ pub struct FunctionDefinition { pub enum FunctionDefinitionKind { /// Actual definition block and surrounding signature range Local(Block, Metadata), - /// Return Type - Extern(TypeKind), + Extern, } #[derive(Debug)] diff --git a/reid/src/mir/types.rs b/reid/src/mir/types.rs index fa6089f..6df91ed 100644 --- a/reid/src/mir/types.rs +++ b/reid/src/mir/types.rs @@ -64,11 +64,11 @@ impl ReturnType for FunctionCall { } } -impl ReturnType for FunctionDefinition { - fn return_type(&self) -> Result { - match &self.kind { - FunctionDefinitionKind::Local(block, _) => block.return_type(), - FunctionDefinitionKind::Extern(type_kind) => Ok(type_kind.clone()), - } - } -} +// impl ReturnType for FunctionDefinition { +// fn return_type(&self) -> Result { +// match &self.kind { +// FunctionDefinitionKind::Local(block, _) => block.return_type(), +// FunctionDefinitionKind::Extern(type_kind) => Ok(type_kind.clone()), +// } +// } +// }