diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 2fa3dc4..a871ffa 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -225,8 +225,16 @@ impl ast::Expression { mir::TypeKind::Vague(mir::VagueType::Unknown), name.clone(), ), - ast::ExpressionKind::Borrow(_) => todo!(), - ast::ExpressionKind::Deref(_) => todo!(), + ast::ExpressionKind::Borrow(name) => mir::ExprKind::Borrow(NamedVariableRef( + mir::TypeKind::Vague(mir::VagueType::Unknown), + name.clone(), + self.1.as_meta(module_id), + )), + ast::ExpressionKind::Deref(name) => mir::ExprKind::Deref(NamedVariableRef( + mir::TypeKind::Vague(mir::VagueType::Unknown), + name.clone(), + self.1.as_meta(module_id), + )), }; mir::Expression(kind, self.1.as_meta(module_id)) diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index de7394e..63ee507 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -935,6 +935,8 @@ impl mir::Expression { TypeKind::CustomType(name.clone()), )) } + mir::ExprKind::Borrow(named_variable_ref) => todo!(), + mir::ExprKind::Deref(named_variable_ref) => todo!(), }; if let Some(value) = &value { value.instr().maybe_location(&mut scope.block, location); @@ -1111,6 +1113,9 @@ impl TypeKind { TypeKind::Ptr(type_kind) => { Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs))) } + TypeKind::Borrow(type_kind) => { + Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs))) + } } } } diff --git a/reid/src/mir/display.rs b/reid/src/mir/display.rs index 4b34f16..0b4e2d7 100644 --- a/reid/src/mir/display.rs +++ b/reid/src/mir/display.rs @@ -208,6 +208,8 @@ impl Display for ExprKind { write_access(f, name)?; write!(f, "<{}>", type_kind) } + ExprKind::Borrow(var_ref) => write!(f, "&{}", var_ref), + ExprKind::Deref(var_ref) => write!(f, "*{}", var_ref), } } } diff --git a/reid/src/mir/impl.rs b/reid/src/mir/impl.rs index d99fd6a..22428b3 100644 --- a/reid/src/mir/impl.rs +++ b/reid/src/mir/impl.rs @@ -12,6 +12,7 @@ pub enum ReturnTypeOther { EmptyBlock(Metadata), NoBlockReturn(Metadata), IndexingNonArray(Metadata), + DerefNonBorrow(Metadata), } impl TypeKind { @@ -47,8 +48,9 @@ impl TypeKind { TypeKind::StringPtr => 32, TypeKind::Array(type_kind, len) => type_kind.size_of() * len, TypeKind::CustomType(_) => 32, - TypeKind::Ptr(inner) => 64, + TypeKind::Ptr(_) => 64, TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"), + TypeKind::Borrow(_) => 64, } } @@ -69,8 +71,9 @@ impl TypeKind { TypeKind::StringPtr => 32, TypeKind::Array(type_kind, _) => type_kind.alignment(), TypeKind::CustomType(_) => 32, - TypeKind::Ptr(type_kind) => 64, + TypeKind::Ptr(_) => 64, TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"), + TypeKind::Borrow(_) => 64, } } } @@ -222,6 +225,17 @@ impl Expression { } Accessed(_, type_kind, _) => Ok((ReturnKind::Soft, type_kind.clone())), Struct(name, _) => Ok((ReturnKind::Soft, TypeKind::CustomType(name.clone()))), + Borrow(var) => { + let ret_type = var.return_type()?; + Ok((ret_type.0, TypeKind::Borrow(Box::new(ret_type.1)))) + } + Deref(var) => { + let ret_type = var.return_type()?; + match ret_type { + (_, TypeKind::Borrow(type_kind)) => Ok((ret_type.0, *type_kind)), + _ => Err(ReturnTypeOther::DerefNonBorrow(var.2)), + } + } } } @@ -230,13 +244,15 @@ impl Expression { ExprKind::Variable(var_ref) => Some(var_ref), ExprKind::Indexed(lhs, _, _) => lhs.backing_var(), ExprKind::Accessed(lhs, _, _) => lhs.backing_var(), + ExprKind::Borrow(var) => Some(var), + ExprKind::Deref(var) => Some(var), + ExprKind::Block(block) => block.backing_var(), ExprKind::Array(_) => None, ExprKind::Struct(_, _) => None, ExprKind::Literal(_) => None, ExprKind::BinOp(_, _, _) => None, ExprKind::FunctionCall(_) => None, ExprKind::If(_) => None, - ExprKind::Block(block) => block.backing_var(), } } } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 41694fb..ad81646 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -110,6 +110,8 @@ pub enum TypeKind { #[error("{0}")] CustomType(String), #[error("Ptr({0})")] + Borrow(Box), + #[error("Ptr({0})")] Ptr(Box), #[error(transparent)] Vague(#[from] VagueType), @@ -245,6 +247,8 @@ pub enum ExprKind { FunctionCall(FunctionCall), If(IfExpression), Block(Block), + Borrow(NamedVariableRef), + Deref(NamedVariableRef), } #[derive(Debug)] diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index a57a120..3042355 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -627,6 +627,8 @@ impl Expression { } Ok(TypeKind::CustomType(struct_name.clone())) } + ExprKind::Borrow(named_variable_ref) => todo!(), + ExprKind::Deref(named_variable_ref) => todo!(), } } } diff --git a/reid/src/mir/typeinference.rs b/reid/src/mir/typeinference.rs index 3d357f6..c45721b 100644 --- a/reid/src/mir/typeinference.rs +++ b/reid/src/mir/typeinference.rs @@ -364,6 +364,8 @@ impl Expression { .from_type(&TypeKind::CustomType(struct_name.clone())) .unwrap()) } + ExprKind::Borrow(named_variable_ref) => todo!(), + ExprKind::Deref(named_variable_ref) => todo!(), } } }