Add AST->MIR for borrow/deref

This commit is contained in:
Sofia 2025-07-20 21:34:47 +03:00
parent 93b4de9230
commit 26818cec96
7 changed files with 44 additions and 5 deletions

View File

@ -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))

View File

@ -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)))
}
}
}
}

View File

@ -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),
}
}
}

View File

@ -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(),
}
}
}

View File

@ -110,6 +110,8 @@ pub enum TypeKind {
#[error("{0}")]
CustomType(String),
#[error("Ptr({0})")]
Borrow(Box<TypeKind>),
#[error("Ptr({0})")]
Ptr(Box<TypeKind>),
#[error(transparent)]
Vague(#[from] VagueType),
@ -245,6 +247,8 @@ pub enum ExprKind {
FunctionCall(FunctionCall),
If(IfExpression),
Block(Block),
Borrow(NamedVariableRef),
Deref(NamedVariableRef),
}
#[derive(Debug)]

View File

@ -627,6 +627,8 @@ impl Expression {
}
Ok(TypeKind::CustomType(struct_name.clone()))
}
ExprKind::Borrow(named_variable_ref) => todo!(),
ExprKind::Deref(named_variable_ref) => todo!(),
}
}
}

View File

@ -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!(),
}
}
}