diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 7aadbfc..dfcc59e 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -256,6 +256,7 @@ impl mir::Module { let mut typedefs = self.typedefs.clone(); typedefs.sort_by(|a, b| b.source_module.cmp(&a.source_module)); + dbg!(&self.module_id, &typedefs); for typedef in typedefs { let type_key = CustomTypeKey(typedef.name.clone(), typedef.source_module); let type_value = match &typedef.kind { @@ -835,7 +836,6 @@ impl mir::Expression { .expect("index returned none!") .instr(); - dbg!(&ty); let TypeKind::CodegenPtr(inner) = ty else { panic!(); }; diff --git a/reid/src/mir/linker.rs b/reid/src/mir/linker.rs index 5252432..2d9b249 100644 --- a/reid/src/mir/linker.rs +++ b/reid/src/mir/linker.rs @@ -103,6 +103,8 @@ impl<'map> Pass for LinkerPass<'map> { let mut modules_to_process: Vec>> = modules.values().cloned().collect(); + let mut already_imported_types = HashSet::::new(); + while let Some(module) = modules_to_process.pop() { let mut importer_module = module.borrow_mut(); @@ -232,28 +234,30 @@ impl<'map> Pass for LinkerPass<'map> { } } - fn import_type(_: &String, ty: &TypeKind) -> (TypeKind, Vec) { + fn import_type(ty: &TypeKind) -> Vec { let mut imported_types = Vec::new(); - let ty = match &ty { - TypeKind::CustomType(key) => { - imported_types.push(key.clone()); - TypeKind::CustomType(key.clone()) - } - _ => ty.clone(), + match &ty { + TypeKind::CustomType(key) => imported_types.push(key.clone()), + TypeKind::Borrow(ty, _) => imported_types.extend(import_type(ty)), + TypeKind::Array(ty, _) => imported_types.extend(import_type(ty)), + TypeKind::UserPtr(ty) => imported_types.extend(import_type(ty)), + TypeKind::CodegenPtr(ty) => imported_types.extend(import_type(ty)), + _ => {} }; - (ty, imported_types) + imported_types } let mut imported_types = Vec::new(); - let (return_type, types) = import_type(&imported_mod_name, &func.return_type); + let types = import_type(&func.return_type); + let return_type = func.return_type.clone(); imported_types.extend(types); let mut param_tys = Vec::new(); for (param_name, param_ty) in &func.parameters { - let (param_type, types) = import_type(&imported_mod_name, ¶m_ty); + let types = import_type(¶m_ty); imported_types.extend(types); - param_tys.push((param_name.clone(), param_type)); + param_tys.push((param_name.clone(), param_ty.clone())); } fn find_inner_types( @@ -303,6 +307,14 @@ impl<'map> Pass for LinkerPass<'map> { seen.extend(inner); } + // TODO: Unable to import same-named type from multiple places.. + let seen = seen + .difference(&already_imported_types) + .cloned() + .collect::>(); + + already_imported_types.extend(seen.clone()); + for typekey in seen.into_iter() { let typedef = imported_mod_typedefs .iter() @@ -317,7 +329,7 @@ impl<'map> Pass for LinkerPass<'map> { name: func_name, is_pub: false, is_imported: false, - return_type: return_type, + return_type, parameters: param_tys, kind: super::FunctionDefinitionKind::Extern(true), source: imported_mod_id, diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 70bd348..9d8dfc0 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -774,21 +774,15 @@ impl TypeKind { fn is_known(&self, refs: &TypeRefs, state: &TypecheckPassState) -> Result<(), ErrorKind> { match &self { TypeKind::Array(type_kind, _) => type_kind.as_ref().is_known(refs, state), - TypeKind::CustomType(custom_type_key) => { - if custom_type_key.1 == state.module_id.unwrap() { - state - .scope - .types - .get(custom_type_key) - .map(|_| ()) - .ok_or(ErrorKind::NoSuchType( - custom_type_key.0.clone(), - state.module_id.unwrap(), - )) - } else { - Ok(()) - } - } + TypeKind::CustomType(custom_type_key) => state + .scope + .types + .get(custom_type_key) + .map(|_| ()) + .ok_or(ErrorKind::NoSuchType( + custom_type_key.0.clone(), + state.module_id.unwrap(), + )), TypeKind::Borrow(type_kind, _) => type_kind.is_known(refs, state), TypeKind::UserPtr(type_kind) => type_kind.is_known(refs, state), TypeKind::CodegenPtr(type_kind) => type_kind.is_known(refs, state), diff --git a/reid/src/mir/typeinference.rs b/reid/src/mir/typeinference.rs index 4b729b8..35bf42f 100644 --- a/reid/src/mir/typeinference.rs +++ b/reid/src/mir/typeinference.rs @@ -322,7 +322,6 @@ impl Expression { // Check that the resolved type is at least a struct, no // need for further resolution. let kind = expr_ty.resolve_weak().unwrap(); - dbg!(&state.scope); match kind { CustomType(key) => { let struct_ty = state