From 965ad5797f350024a17404b46be1959a99a80430 Mon Sep 17 00:00:00 2001 From: sofia Date: Wed, 16 Jul 2025 23:49:28 +0300 Subject: [PATCH] Implement rudamentary borrow --- reid/src/codegen.rs | 30 ++++++++++++++++++++++-------- reid/src/mir/impl.rs | 14 ++++++++++++-- reid/src/mir/mod.rs | 2 ++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 544d528..be75935 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -415,8 +415,14 @@ impl mir::Expression { } } mir::ExprKind::Indexed(expression, val_t, idx_expr) => { - let array = expression.codegen(scope, state)?; - let idx = idx_expr.codegen(scope, state)?; + dbg!(&expression, &idx_expr); + let array = expression + .codegen(scope, state) + .expect("array returned none!"); + let idx = idx_expr + .codegen(scope, state) + .expect("index returned none!"); + let mut ptr = scope .block .build(Instr::GetElemPtr(array, vec![idx])) @@ -468,14 +474,19 @@ impl mir::Expression { Some(array) } mir::ExprKind::Accessed(expression, type_kind, field) => { - let struct_val = expression.codegen(scope, &mut state.load(true))?; + let struct_val = expression.codegen(scope, &mut state.load(true)).unwrap(); - let struct_ty = expression.return_type().ok()?.1.known().ok()?; - let TypeKind::CustomType(name) = struct_ty else { - return None; + let struct_ty = expression + .return_type() + .map(|r| r.1.known()) + .unwrap() + .unwrap(); + + let TypeKind::CustomType(name) = struct_ty.deref_borrow() else { + panic!("tried accessing non-custom-type"); }; - let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&name)?; - let idx = struct_ty.find_index(field)?; + let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&name).unwrap(); + let idx = struct_ty.find_index(field).unwrap(); let mut value = scope .block @@ -644,6 +655,9 @@ impl TypeKind { TypeDefinitionKind::Struct(_) => Type::Ptr(Box::new(custom_t)), } } + TypeKind::Borrow(type_kind) => { + Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs))) + } } } } diff --git a/reid/src/mir/impl.rs b/reid/src/mir/impl.rs index 38f9c1f..e175b68 100644 --- a/reid/src/mir/impl.rs +++ b/reid/src/mir/impl.rs @@ -149,7 +149,7 @@ impl Expression { Indexed(expression, _, _) => { let expr_type = expression.return_type()?; if let (_, TypeKind::Array(elem_ty, _)) = expr_type { - Ok((ReturnKind::Soft, *elem_ty)) + Ok((ReturnKind::Soft, TypeKind::Borrow(Box::new(*elem_ty)))) } else { Err(ReturnTypeOther::IndexingNonArray(expression.1)) } @@ -165,7 +165,10 @@ impl Expression { TypeKind::Array(Box::new(first.1), expressions.len() as u64), )) } - Accessed(_, type_kind, _) => Ok((ReturnKind::Soft, type_kind.clone())), + Accessed(_, type_kind, _) => Ok(( + ReturnKind::Soft, + TypeKind::Borrow(Box::new(type_kind.clone())), + )), Struct(name, _) => Ok((ReturnKind::Soft, TypeKind::CustomType(name.clone()))), } } @@ -267,6 +270,13 @@ impl TypeKind { _ => resolved, } } + + pub fn deref_borrow(&self) -> TypeKind { + match self { + TypeKind::Borrow(type_kind) => *type_kind.clone(), + _ => self.clone(), + } + } } #[derive(Debug, Clone, thiserror::Error)] diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 3721f76..96a9da7 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -65,6 +65,8 @@ pub enum TypeKind { StringPtr, #[error("[{0}; {1}]")] Array(Box, u64), + #[error("Borrow({0})")] + Borrow(Box), #[error("{0}")] CustomType(String), #[error(transparent)]