Update typerefs in statements and expressions as well for extern types

This commit is contained in:
Sofia 2025-07-26 12:55:44 +03:00
parent 59e4c38770
commit 58cc633f98
4 changed files with 70 additions and 16 deletions

View File

@ -1,12 +0,0 @@
import std::print;
import std::new_string;
import std::String;
fn otus() -> String {
return new_string();
}
fn main() -> u8 {
return 0;
}

View File

@ -0,0 +1,19 @@
import std::print;
import std::from_str;
import std::concat_strings;
import std::free_string;
import std::String;
fn otus(param: &mut String) {
let b: String = from_str(" bello");
concat_strings(param, b);
free_string(&b);
}
fn main() -> u8 {
let mut otus = from_str("hello");
otus(&mut otus);
print(otus);
return 0;
}

View File

@ -326,14 +326,59 @@ impl<'map> Pass for LinkerPass<'map> {
fn function(
&mut self,
function: &mut FunctionDefinition,
mut state: PassState<Self::Data, Self::TError>,
state: PassState<Self::Data, Self::TError>,
) -> 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);
for param in function.parameters.iter_mut() {
param.1 = param.1.update_imported(extern_types, mod_id);
}
}
}
Ok(())
}
fn stmt(&mut self, stmt: &mut super::Statement, state: PassState<Self::Data, Self::TError>) -> PassResult {
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 {
match &mut stmt.0 {
super::StmtKind::Let(var_ref, _, _) => {
var_ref.0 = var_ref.0.update_imported(extern_types, mod_id);
}
_ => {}
}
}
Ok(())
}
fn expr(&mut self, expr: &mut super::Expression, state: PassState<Self::Data, Self::TError>) -> PassResult {
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 {
match &mut expr.0 {
super::ExprKind::Variable(var_ref) => {
var_ref.0 = var_ref.0.update_imported(extern_types, mod_id);
}
super::ExprKind::Indexed(.., type_kind, _) => {
*type_kind = type_kind.update_imported(extern_types, mod_id)
}
super::ExprKind::Accessed(.., type_kind, _) => {
*type_kind = type_kind.update_imported(extern_types, mod_id)
}
super::ExprKind::BinOp(.., type_kind) => *type_kind = type_kind.update_imported(extern_types, mod_id),
super::ExprKind::Borrow(var_ref, _) => {
var_ref.0 = var_ref.0.update_imported(extern_types, mod_id);
}
super::ExprKind::Deref(var_ref) => {
var_ref.0 = var_ref.0.update_imported(extern_types, mod_id);
}
super::ExprKind::CastTo(_, type_kind) => *type_kind = type_kind.update_imported(extern_types, mod_id),
_ => {}
}
}
Ok(())

View File

@ -50,8 +50,10 @@ impl<'t> Pass for TypeCheck<'t> {
}
}
if let Some(_) = defmap.insert(&typedef.name, typedef) {
state.ok::<_, Infallible>(Err(ErrorKind::DuplicateTypeName(name.clone())), meta.clone());
if typedef.source_module == module.module_id || typedef.importer == Some(module.module_id) {
if let Some(_) = defmap.insert(&typedef.name, typedef) {
state.ok::<_, Infallible>(Err(ErrorKind::DuplicateTypeName(name.clone())), meta.clone());
}
}
}