From 59e4c3877048284a184ce70f7e7c5534d08c960e Mon Sep 17 00:00:00 2001 From: sofia Date: Sat, 26 Jul 2025 01:34:37 +0300 Subject: [PATCH] Fix type references for function return types .. for now --- reid/src/mir/linker.rs | 79 +++++++++++++++++++++++++++++++++++++----- reid/src/mir/pass.rs | 4 +++ 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/reid/src/mir/linker.rs b/reid/src/mir/linker.rs index cccb093..3022663 100644 --- a/reid/src/mir/linker.rs +++ b/reid/src/mir/linker.rs @@ -8,9 +8,10 @@ use std::{ }; use crate::{ + codegen::scope, compile_module, error_raporting::{ErrorModules, ReidError}, - mir::{CustomTypeKey, SourceModuleId, TypeDefinition, TypeKind}, + mir::{CustomTypeKey, FunctionDefinitionKind, SourceModuleId, TypeDefinition, TypeKind}, parse_module, }; @@ -65,10 +66,15 @@ pub struct LinkerPass<'map> { pub is_lib: bool, } -type LinkerPassState<'st, 'sc> = PassState<'st, 'sc, (), ErrorKind>; +#[derive(Default, Clone)] +pub struct LinkerState { + extern_imported_types: HashMap>, +} + +type LinkerPassState<'st, 'sc> = PassState<'st, 'sc, LinkerState, ErrorKind>; impl<'map> Pass for LinkerPass<'map> { - type Data = (); + type Data = LinkerState; type TError = ErrorKind; fn context(&mut self, context: &mut Context, mut state: LinkerPassState) -> PassResult { let mains = context @@ -106,6 +112,7 @@ impl<'map> Pass for LinkerPass<'map> { let mut already_imported_types = HashSet::::new(); while let Some(module) = modules_to_process.pop() { + let mut extern_types = HashMap::new(); let mut importer_module = module.borrow_mut(); for import in importer_module.imports.clone() { @@ -238,11 +245,12 @@ impl<'map> Pass for LinkerPass<'map> { } let mut seen = HashSet::new(); - let mut extern_types = HashSet::new(); + let mut current_extern_types = HashSet::new(); seen.extend(imported_types.clone().iter().map(|t| t.0.clone())); - extern_types.extend(imported_types.clone().iter().filter(|t| t.1).map(|t| t.0.clone())); - dbg!(&imported_types); - dbg!(&extern_types); + current_extern_types.extend(imported_types.clone().iter().filter(|t| t.1).map(|t| t.0.clone())); + for extern_type in ¤t_extern_types { + extern_types.insert(extern_type.0.clone(), extern_type.1); + } let imported_mod_id = imported.module_id; let imported_mod_typedefs = &mut imported.typedefs; @@ -265,7 +273,7 @@ impl<'map> Pass for LinkerPass<'map> { already_imported_types.extend(seen.clone()); for typekey in &already_imported_types { - if extern_types.contains(typekey) { + if current_extern_types.contains(typekey) { let module_id = importer_module.module_id; let typedef = importer_module .typedefs @@ -285,7 +293,7 @@ impl<'map> Pass for LinkerPass<'map> { .unwrap() .clone(); - if extern_types.contains(&typekey) { + if current_extern_types.contains(&typekey) { typedef = TypeDefinition { importer: Some(importer_module.module_id), ..typedef @@ -296,6 +304,11 @@ impl<'map> Pass for LinkerPass<'map> { } dbg!(&importer_module.typedefs); } + state + .scope + .data + .extern_imported_types + .insert(importer_module.module_id, extern_types); } let mut modules: Vec = modules @@ -309,6 +322,54 @@ impl<'map> Pass for LinkerPass<'map> { Ok(()) } + + fn function( + &mut self, + function: &mut FunctionDefinition, + mut state: PassState, + ) -> PassResult { + if matches!(function.kind, FunctionDefinitionKind::Local(_, _)) { + let mod_id = state.scope.module_id.unwrap(); + let extern_types = &state.scope.data.extern_imported_types.get(&mod_id); + if let Some(extern_types) = extern_types { + function.return_type = function.return_type.update_imported(*extern_types, mod_id); + dbg!(&function.return_type); + } + } + Ok(()) + } +} + +impl TypeKind { + fn update_imported( + &self, + extern_types: &HashMap, + importer_mod_id: SourceModuleId, + ) -> TypeKind { + match &self { + TypeKind::Array(type_kind, len) => { + TypeKind::Array(Box::new(type_kind.update_imported(extern_types, importer_mod_id)), *len) + } + TypeKind::CustomType(custom_type_key) => { + if let Some(mod_id) = extern_types.get(&custom_type_key.0) { + TypeKind::CustomType(CustomTypeKey(custom_type_key.0.clone(), *mod_id)) + } else { + self.clone() + } + } + TypeKind::Borrow(type_kind, mutable) => TypeKind::Borrow( + Box::new(type_kind.update_imported(extern_types, importer_mod_id)), + *mutable, + ), + TypeKind::UserPtr(type_kind) => { + TypeKind::UserPtr(Box::new(type_kind.update_imported(extern_types, importer_mod_id))) + } + TypeKind::CodegenPtr(type_kind) => { + TypeKind::CodegenPtr(Box::new(type_kind.update_imported(extern_types, importer_mod_id))) + } + _ => self.clone(), + } + } } fn import_type(ty: &TypeKind, usable_import: bool) -> Vec<(CustomTypeKey, bool)> { diff --git a/reid/src/mir/pass.rs b/reid/src/mir/pass.rs index 8c5ab5d..e2c7321 100644 --- a/reid/src/mir/pass.rs +++ b/reid/src/mir/pass.rs @@ -121,6 +121,7 @@ pub type BinopMap = Storage; #[derive(Clone, Default, Debug)] pub struct Scope { + pub module_id: Option, pub binops: BinopMap, pub function_returns: Storage, pub variables: Storage, @@ -133,6 +134,7 @@ pub struct Scope { impl Scope { pub fn inner(&self) -> Scope { Scope { + module_id: self.module_id, function_returns: self.function_returns.clone(), variables: self.variables.clone(), binops: self.binops.clone(), @@ -356,6 +358,8 @@ impl Context { impl Module { fn pass(&mut self, pass: &mut T, state: &mut State, scope: &mut Scope) -> PassResult { + scope.module_id = Some(self.module_id); + for typedef in &self.typedefs { scope .types