From 3d73c52cb47a3472fe3431996ff6bbe5bb4870c8 Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 22 Jul 2025 19:31:35 +0300 Subject: [PATCH] Fix casting within setters --- reid-llvm-lib/src/builder.rs | 3 +- reid/lib/std.reid | 2 +- reid/src/ast/process.rs | 4 +- reid/src/codegen.rs | 92 +++++++++++++++++++++-------------- reid/src/mir/fmt.rs | 2 +- reid/src/mir/implement.rs | 2 +- reid/src/mir/linker.rs | 18 +++---- reid/src/mir/mod.rs | 4 +- reid/src/mir/pass.rs | 10 ++-- reid/src/mir/typecheck.rs | 4 +- reid/src/mir/typeinference.rs | 4 +- reid_src/hello_world.reid | 2 + 12 files changed, 84 insertions(+), 63 deletions(-) diff --git a/reid-llvm-lib/src/builder.rs b/reid-llvm-lib/src/builder.rs index a614d08..eaf875e 100644 --- a/reid-llvm-lib/src/builder.rs +++ b/reid-llvm-lib/src/builder.rs @@ -389,7 +389,6 @@ impl Builder { return Err(()); // TODO error: invalid amount of params } for (a, b) in param_types.iter().zip(params) { - dbg!(&a, &b.get_type(&self)); if *a != b.get_type(&self)? { return Err(()); // TODO error: params do not match } @@ -479,7 +478,7 @@ impl Builder { Instr::SIToFP(instr, ty) => instr.cast_to(self, &ty).map(|_| ()), Instr::PtrToInt(instr, ty) => instr.cast_to(self, &ty).map(|_| ()), Instr::IntToPtr(instr, ty) => instr.cast_to(self, &ty).map(|_| ()), - Instr::BitCast(instr, ty) => Ok(()), + Instr::BitCast(..) => Ok(()), } } } diff --git a/reid/lib/std.reid b/reid/lib/std.reid index 5708c95..c0e1c9a 100644 --- a/reid/lib/std.reid +++ b/reid/lib/std.reid @@ -41,7 +41,7 @@ pub fn add_char(string: &mut String, c: char) { (*string).inner = allocate((*string).max_length) as *char; } (*string).inner[(*string).length] = c; - (*string).inner[((*string).length + 1)] = '\0'; + (((*string).inner) as *u8)[((*string).length + 1)] = 0; (*string).length = (*string).length + 1; } diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index f0378a7..d47e690 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -4,7 +4,7 @@ use crate::{ ast::{self}, mir::{ self, ModuleMap, NamedVariableRef, SourceModuleId, StmtKind, StructField, StructType, - TypeKey, + CustomTypeKey, }, }; @@ -322,7 +322,7 @@ impl ast::TypeKind { mir::TypeKind::Array(Box::new(type_kind.clone().into_mir(source_mod)), *length) } ast::TypeKind::Custom(name) => { - mir::TypeKind::CustomType(TypeKey(name.clone(), source_mod)) + mir::TypeKind::CustomType(CustomTypeKey(name.clone(), source_mod)) } ast::TypeKind::Borrow(type_kind, mutable) => { mir::TypeKind::Borrow(Box::new(type_kind.clone().into_mir(source_mod)), *mutable) diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index b9fa801..bb353b8 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -18,8 +18,8 @@ use crate::{ error_raporting::ErrorModules, lexer::{FullToken, Position}, mir::{ - self, implement::TypeCategory, Metadata, ModuleMap, NamedVariableRef, SourceModuleId, - StructField, StructType, TypeDefinition, TypeDefinitionKind, TypeKey, TypeKind, + self, implement::TypeCategory, CustomTypeKey, Metadata, ModuleMap, NamedVariableRef, + SourceModuleId, StructField, StructType, TypeDefinition, TypeDefinitionKind, TypeKind, VagueLiteral, }, }; @@ -59,7 +59,7 @@ struct ModuleCodegen<'ctx> { module: Module<'ctx>, tokens: &'ctx Vec, debug_types: Option>, - type_values: HashMap, + type_values: HashMap, } impl<'ctx> std::fmt::Debug for ModuleCodegen<'ctx> { @@ -77,7 +77,7 @@ pub struct Scope<'ctx, 'scope> { function: &'ctx StackFunction<'ctx>, block: Block<'ctx>, types: &'scope HashMap, - type_values: &'scope HashMap, + type_values: &'scope HashMap, functions: &'scope HashMap>, stack_values: HashMap, debug: Option>, @@ -169,7 +169,7 @@ impl<'ctx, 'a> Scope<'ctx, 'a> { old_block } - fn get_typedef(&self, key: &TypeKey) -> Option<&TypeDefinition> { + fn get_typedef(&self, key: &CustomTypeKey) -> Option<&TypeDefinition> { self.type_values.get(key).and_then(|v| self.types.get(v)) } } @@ -255,7 +255,7 @@ impl mir::Module { typedefs.sort_by(|a, b| b.source_module.cmp(&a.source_module)); for typedef in typedefs { - let type_key = TypeKey(typedef.name.clone(), typedef.source_module); + let type_key = CustomTypeKey(typedef.name.clone(), typedef.source_module); let type_value = match &typedef.kind { TypeDefinitionKind::Struct(StructType(fields)) => { module.custom_type(CustomTypeKind::NamedStruct(NamedStruct( @@ -391,8 +391,6 @@ impl mir::Module { &debug_scope, mir_function.signature().into_debug(tokens, compile_unit), ) { - dbg!(mir_function.name.clone()); - dbg!(p_name.clone()); // debug.metadata( // &location, // DebugMetadata::ParamVar(DebugParamVariable { @@ -956,7 +954,7 @@ impl mir::Expression { } } mir::ExprKind::Struct(name, items) => { - let type_key = TypeKey(name.clone(), scope.module_id); + let type_key = CustomTypeKey(name.clone(), scope.module_id); let struct_ty = Type::CustomType(*scope.type_values.get(&type_key)?); let load_n = format!("{}.load", name); @@ -1062,35 +1060,57 @@ impl mir::Expression { } mir::ExprKind::CastTo(expression, type_kind) => { let val = expression.codegen(scope, state)?; + if val.1 == *type_kind { Some(val) - } else if let (TypeKind::UserPtr(_), TypeKind::UserPtr(_)) = (&val.1, type_kind) { - Some(StackValue( - val.0.derive( - scope - .block - .build(Instr::BitCast( - val.instr(), - type_kind.get_type(scope.type_values, scope.types), - )) - .unwrap(), - ), - type_kind.clone(), - )) } else { - let cast_instr = val - .1 - .get_type(scope.type_values, scope.types) - .cast_instruction( - val.instr(), - &type_kind.get_type(scope.type_values, scope.types), - ) - .unwrap(); + match (&val.1, type_kind) { + (TypeKind::CodegenPtr(inner), TypeKind::UserPtr(_)) => match *inner.clone() + { + TypeKind::UserPtr(_) => Some(StackValue( + val.0.derive( + scope + .block + .build(Instr::BitCast( + val.instr(), + Type::Ptr(Box::new( + type_kind.get_type(scope.type_values, scope.types), + )), + )) + .unwrap(), + ), + TypeKind::CodegenPtr(Box::new(type_kind.clone())), + )), + _ => panic!(), + }, + (TypeKind::UserPtr(_), TypeKind::UserPtr(_)) => Some(StackValue( + val.0.derive( + scope + .block + .build(Instr::BitCast( + val.instr(), + type_kind.get_type(scope.type_values, scope.types), + )) + .unwrap(), + ), + type_kind.clone(), + )), + _ => { + let cast_instr = val + .1 + .get_type(scope.type_values, scope.types) + .cast_instruction( + val.instr(), + &type_kind.get_type(scope.type_values, scope.types), + ) + .unwrap(); - Some(StackValue( - val.0.derive(scope.block.build(cast_instr).unwrap()), - type_kind.clone(), - )) + Some(StackValue( + val.0.derive(scope.block.build(cast_instr).unwrap()), + type_kind.clone(), + )) + } + } } } }; @@ -1257,7 +1277,7 @@ impl mir::Literal { impl TypeKind { fn get_type( &self, - type_vals: &HashMap, + type_vals: &HashMap, typedefs: &HashMap, ) -> Type { match &self { @@ -1321,7 +1341,7 @@ impl TypeKind { scope: DebugProgramValue, debug_info: &DebugInformation, debug_types: &HashMap, - type_values: &HashMap, + type_values: &HashMap, types: &HashMap, local_mod: SourceModuleId, tokens: &Vec, diff --git a/reid/src/mir/fmt.rs b/reid/src/mir/fmt.rs index f125b3b..6e1452d 100644 --- a/reid/src/mir/fmt.rs +++ b/reid/src/mir/fmt.rs @@ -345,7 +345,7 @@ impl Display for TypeKind { Display::fmt(len, f)?; f.write_char(']') } - TypeKind::CustomType(TypeKey(name, mod_id)) => write!(f, "{}:{}", mod_id, name), + TypeKind::CustomType(CustomTypeKey(name, mod_id)) => write!(f, "{}:{}", mod_id, name), TypeKind::Borrow(type_kind, false) => { write!(f, "&")?; Display::fmt(type_kind, f) diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index 524915d..4265ac1 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -412,7 +412,7 @@ impl Expression { Accessed(_, type_kind, _) => Ok((ReturnKind::Soft, type_kind.clone())), Struct(name, _) => Ok(( ReturnKind::Soft, - TypeKind::CustomType(TypeKey(name.clone(), mod_id)), + TypeKind::CustomType(CustomTypeKey(name.clone(), mod_id)), )), Borrow(var, mutable) => { let ret_type = var.return_type()?; diff --git a/reid/src/mir/linker.rs b/reid/src/mir/linker.rs index 6a1f9ac..3e0889f 100644 --- a/reid/src/mir/linker.rs +++ b/reid/src/mir/linker.rs @@ -10,7 +10,7 @@ use std::{ use crate::{ compile_module, error_raporting::{ErrorModules, ReidError}, - mir::{SourceModuleId, TypeDefinition, TypeKey, TypeKind}, + mir::{SourceModuleId, TypeDefinition, CustomTypeKey, TypeKind}, parse_module, }; @@ -232,7 +232,7 @@ impl<'map> Pass for LinkerPass<'map> { } } - fn import_type(base: &String, ty: &TypeKind) -> (TypeKind, Vec) { + fn import_type(base: &String, ty: &TypeKind) -> (TypeKind, Vec) { let mut imported_types = Vec::new(); let ty = match &ty { TypeKind::CustomType(key) => { @@ -258,9 +258,9 @@ impl<'map> Pass for LinkerPass<'map> { fn find_inner_types( typedef: &TypeDefinition, - mut seen: HashSet, + mut seen: HashSet, mod_id: SourceModuleId, - ) -> Vec { + ) -> Vec { match &typedef.kind { crate::mir::TypeDefinitionKind::Struct(struct_type) => { let typenames = struct_type @@ -268,18 +268,18 @@ impl<'map> Pass for LinkerPass<'map> { .iter() .filter(|t| matches!(t.1, TypeKind::CustomType(..))) .map(|t| match &t.1 { - TypeKind::CustomType(TypeKey(t, _)) => t, + TypeKind::CustomType(CustomTypeKey(t, _)) => t, _ => panic!(), }) .cloned() .collect::>(); for typename in typenames { - if seen.contains(&TypeKey(typename.clone(), mod_id)) { + if seen.contains(&CustomTypeKey(typename.clone(), mod_id)) { continue; } let inner = find_inner_types(typedef, seen.clone(), mod_id); - seen.insert(TypeKey(typename, mod_id)); + seen.insert(CustomTypeKey(typename, mod_id)); seen.extend(inner); } @@ -297,7 +297,7 @@ impl<'map> Pass for LinkerPass<'map> { for typekey in imported_types.clone() { let typedef = imported_mod_typedefs .iter() - .find(|ty| TypeKey(ty.name.clone(), imported_mod_id) == typekey) + .find(|ty| CustomTypeKey(ty.name.clone(), imported_mod_id) == typekey) .unwrap(); let inner = find_inner_types(typedef, seen.clone(), imported_mod_id); seen.extend(inner); @@ -306,7 +306,7 @@ impl<'map> Pass for LinkerPass<'map> { for typekey in seen.into_iter() { let typedef = imported_mod_typedefs .iter() - .find(|ty| TypeKey(ty.name.clone(), imported_mod_id) == typekey) + .find(|ty| CustomTypeKey(ty.name.clone(), imported_mod_id) == typekey) .unwrap() .clone(); diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index dc91536..eedd37f 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -78,7 +78,7 @@ impl TokenRange { } #[derive(Hash, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)] -pub struct TypeKey(pub String, pub SourceModuleId); +pub struct CustomTypeKey(pub String, pub SourceModuleId); #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum TypeKind { @@ -103,7 +103,7 @@ pub enum TypeKind { F128PPC, Char, Array(Box, u64), - CustomType(TypeKey), + CustomType(CustomTypeKey), Borrow(Box, bool), UserPtr(Box), CodegenPtr(Box), diff --git a/reid/src/mir/pass.rs b/reid/src/mir/pass.rs index 06d1ece..db63202 100644 --- a/reid/src/mir/pass.rs +++ b/reid/src/mir/pass.rs @@ -117,7 +117,7 @@ impl Storage { pub struct Scope { pub function_returns: Storage, pub variables: Storage, - pub types: Storage, + pub types: Storage, /// Hard Return type of this scope, if inside a function pub return_type_hint: Option, pub data: Data, @@ -134,18 +134,18 @@ impl Scope { } } - pub fn get_struct_type(&self, key: &TypeKey) -> Option<&StructType> { + pub fn get_struct_type(&self, key: &CustomTypeKey) -> Option<&StructType> { let ty = self.types.get(&key)?; match &ty.kind { TypeDefinitionKind::Struct(struct_ty) => Some(struct_ty), } } - pub fn find_type(&self, name: &String) -> Option<&TypeKey> { + pub fn find_type(&self, name: &String) -> Option<&CustomTypeKey> { self.types .0 .iter() - .find(|(TypeKey(n, _), _)| n == name) + .find(|(CustomTypeKey(n, _), _)| n == name) .map(|(key, _)| key) } } @@ -295,7 +295,7 @@ impl Module { scope .types .set( - TypeKey(typedef.name.clone(), self.module_id), + CustomTypeKey(typedef.name.clone(), self.module_id), typedef.clone(), ) .ok(); diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 1db8b6f..3e5e5e6 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -138,7 +138,7 @@ fn check_typedefs_for_recursion<'a, 'b>( match &typedef.kind { TypeDefinitionKind::Struct(StructType(fields)) => { for field_ty in fields.iter().map(|StructField(_, ty, _)| ty) { - if let TypeKind::CustomType(TypeKey(name, _)) = field_ty { + if let TypeKind::CustomType(CustomTypeKey(name, _)) = field_ty { if seen.contains(name) { state.ok::<_, Infallible>( Err(ErrorKind::RecursiveTypeDefinition( @@ -636,7 +636,7 @@ impl Expression { } } ExprKind::Struct(struct_name, items) => { - let type_key = TypeKey(struct_name.clone(), state.module_id.unwrap()); + let type_key = CustomTypeKey(struct_name.clone(), state.module_id.unwrap()); let struct_def = state .scope .get_struct_type(&type_key) diff --git a/reid/src/mir/typeinference.rs b/reid/src/mir/typeinference.rs index dfef2f1..8574da3 100644 --- a/reid/src/mir/typeinference.rs +++ b/reid/src/mir/typeinference.rs @@ -14,7 +14,7 @@ use super::{ typecheck::ErrorKind, typerefs::{ScopeTypeRefs, TypeRef, TypeRefs}, Block, ExprKind, Expression, FunctionDefinition, FunctionDefinitionKind, IfExpression, Module, - ReturnKind, StmtKind, TypeKey, + ReturnKind, StmtKind, CustomTypeKey, TypeKind::*, VagueType::*, }; @@ -339,7 +339,7 @@ impl Expression { } } ExprKind::Struct(struct_name, fields) => { - let type_key = TypeKey(struct_name.clone(), state.module_id.unwrap()); + let type_key = CustomTypeKey(struct_name.clone(), state.module_id.unwrap()); let expected_struct_ty = state .scope .get_struct_type(&type_key) diff --git a/reid_src/hello_world.reid b/reid_src/hello_world.reid index eb2b8d5..542f6e5 100644 --- a/reid_src/hello_world.reid +++ b/reid_src/hello_world.reid @@ -13,5 +13,7 @@ fn main() -> i32 { print(&test); + free_string(&mut test); + return 0; }