Change user-space pointer to different type

This commit is contained in:
Sofia 2025-07-21 13:52:37 +03:00
parent a49105b07a
commit 9db508bd9c
7 changed files with 37 additions and 25 deletions

View File

@ -294,7 +294,7 @@ impl From<ast::TypeKind> for mir::TypeKind {
mir::TypeKind::Borrow(Box::new(mir::TypeKind::from(*type_kind.clone())), *mutable) mir::TypeKind::Borrow(Box::new(mir::TypeKind::from(*type_kind.clone())), *mutable)
} }
ast::TypeKind::Ptr(type_kind) => { ast::TypeKind::Ptr(type_kind) => {
mir::TypeKind::Ptr(Box::new(mir::TypeKind::from(*type_kind.clone()))) mir::TypeKind::UserPtr(Box::new(mir::TypeKind::from(*type_kind.clone())))
} }
} }
} }

View File

@ -368,7 +368,7 @@ impl mir::Module {
p_name.clone(), p_name.clone(),
StackValue( StackValue(
StackValueKind::mutable(p_ty.is_mutable(), alloca), StackValueKind::mutable(p_ty.is_mutable(), alloca),
TypeKind::Ptr(Box::new(p_ty.clone())), TypeKind::CodegenPtr(Box::new(p_ty.clone())),
), ),
); );
@ -511,7 +511,7 @@ impl mir::Statement {
scope.stack_values.insert( scope.stack_values.insert(
name.clone(), name.clone(),
StackValue(stack_value, TypeKind::Ptr(Box::new(value.clone().1))), StackValue(stack_value, TypeKind::CodegenPtr(Box::new(value.clone().1))),
); );
if let Some(debug) = &scope.debug { if let Some(debug) = &scope.debug {
let location = self.1.into_debug(scope.tokens).unwrap(); let location = self.1.into_debug(scope.tokens).unwrap();
@ -595,7 +595,7 @@ impl mir::Expression {
.expect("Variable reference not found?!"); .expect("Variable reference not found?!");
Some({ Some({
if state.should_load { if state.should_load {
if let TypeKind::Ptr(inner) = &v.1 { if let TypeKind::CodegenPtr(inner) = &v.1 {
StackValue( StackValue(
v.0.derive( v.0.derive(
scope scope
@ -715,7 +715,7 @@ impl mir::Expression {
} else { } else {
Some(StackValue( Some(StackValue(
StackValueKind::Immutable(ptr), StackValueKind::Immutable(ptr),
TypeKind::Ptr(Box::new(ret_type_kind)), TypeKind::CodegenPtr(Box::new(ret_type_kind)),
)) ))
} }
} else { } else {
@ -758,7 +758,7 @@ impl mir::Expression {
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
let TypeKind::Ptr(inner) = array_ty else { let TypeKind::CodegenPtr(inner) = array_ty else {
panic!(); panic!();
}; };
let TypeKind::Array(elem_ty, _) = *inner else { let TypeKind::Array(elem_ty, _) = *inner else {
@ -783,7 +783,7 @@ impl mir::Expression {
*elem_ty, *elem_ty,
)) ))
} else { } else {
Some(StackValue(kind.derive(ptr), TypeKind::Ptr(elem_ty))) Some(StackValue(kind.derive(ptr), TypeKind::CodegenPtr(elem_ty)))
} }
} }
mir::ExprKind::Array(expressions) => { mir::ExprKind::Array(expressions) => {
@ -856,7 +856,7 @@ impl mir::Expression {
mir::ExprKind::Accessed(expression, type_kind, field) => { mir::ExprKind::Accessed(expression, type_kind, field) => {
let struct_val = expression.codegen(scope, &state.load(false)).unwrap(); let struct_val = expression.codegen(scope, &state.load(false)).unwrap();
let TypeKind::Ptr(inner) = &struct_val.1 else { let TypeKind::CodegenPtr(inner) = &struct_val.1 else {
panic!("tried accessing non-pointer"); panic!("tried accessing non-pointer");
}; };
let TypeKind::CustomType(name) = *inner.clone() else { let TypeKind::CustomType(name) = *inner.clone() else {
@ -898,7 +898,9 @@ impl mir::Expression {
} else { } else {
Some(StackValue( Some(StackValue(
struct_val.0.derive(value), struct_val.0.derive(value),
TypeKind::Ptr(Box::new(struct_ty.get_field_ty(&field).unwrap().clone())), TypeKind::CodegenPtr(Box::new(
struct_ty.get_field_ty(&field).unwrap().clone(),
)),
)) ))
} }
} }
@ -959,7 +961,7 @@ impl mir::Expression {
.get(&varref.1) .get(&varref.1)
.expect("Variable reference not found?!"); .expect("Variable reference not found?!");
let TypeKind::Ptr(ptr_inner) = &v.1 else { let TypeKind::CodegenPtr(ptr_inner) = &v.1 else {
panic!(); panic!();
}; };
@ -976,7 +978,7 @@ impl mir::Expression {
Some({ Some({
if state.should_load { if state.should_load {
if let TypeKind::Ptr(inner) = *ptr_inner.clone() { if let TypeKind::CodegenPtr(inner) = *ptr_inner.clone() {
StackValue( StackValue(
v.0.derive( v.0.derive(
scope scope
@ -1173,7 +1175,10 @@ impl TypeKind {
let type_val = type_vals.get(n).unwrap().clone(); let type_val = type_vals.get(n).unwrap().clone();
Type::CustomType(type_val) Type::CustomType(type_val)
} }
TypeKind::Ptr(type_kind) => { TypeKind::UserPtr(type_kind) => {
Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs)))
}
TypeKind::CodegenPtr(type_kind) => {
Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs))) Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs)))
} }
TypeKind::Borrow(type_kind, _) => { TypeKind::Borrow(type_kind, _) => {
@ -1223,7 +1228,7 @@ impl TypeKind {
), ),
size_bits: self.size_of(), size_bits: self.size_of(),
}), }),
TypeKind::Ptr(inner) | TypeKind::Borrow(inner, _) => { TypeKind::CodegenPtr(inner) | TypeKind::Borrow(inner, _) => {
DebugTypeData::Pointer(DebugPointerType { DebugTypeData::Pointer(DebugPointerType {
name, name,
pointee: inner.get_debug_type_hard( pointee: inner.get_debug_type_hard(

View File

@ -345,10 +345,14 @@ impl Display for TypeKind {
write!(f, "&mut ")?; write!(f, "&mut ")?;
Display::fmt(type_kind, f) Display::fmt(type_kind, f)
} }
TypeKind::Ptr(type_kind) => { TypeKind::UserPtr(type_kind) => {
write!(f, "*")?; write!(f, "*")?;
Display::fmt(type_kind, f) Display::fmt(type_kind, f)
} }
TypeKind::CodegenPtr(type_kind) => {
write!(f, "CodegenPtr ")?;
Display::fmt(type_kind, f)
}
TypeKind::Vague(vague_type) => Display::fmt(vague_type, f), TypeKind::Vague(vague_type) => Display::fmt(vague_type, f),
} }
} }

View File

@ -46,9 +46,10 @@ impl TypeKind {
TypeKind::StringPtr => 32, TypeKind::StringPtr => 32,
TypeKind::Array(type_kind, len) => type_kind.size_of() * len, TypeKind::Array(type_kind, len) => type_kind.size_of() * len,
TypeKind::CustomType(_) => 32, TypeKind::CustomType(_) => 32,
TypeKind::Ptr(_) => 64, TypeKind::CodegenPtr(_) => 64,
TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"), TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"),
TypeKind::Borrow(_, _) => 64, TypeKind::Borrow(_, _) => 64,
TypeKind::UserPtr(_) => 64,
} }
} }
@ -69,9 +70,10 @@ impl TypeKind {
TypeKind::StringPtr => 32, TypeKind::StringPtr => 32,
TypeKind::Array(type_kind, _) => type_kind.alignment(), TypeKind::Array(type_kind, _) => type_kind.alignment(),
TypeKind::CustomType(_) => 32, TypeKind::CustomType(_) => 32,
TypeKind::Ptr(_) => 64, TypeKind::CodegenPtr(_) => 64,
TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"), TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"),
TypeKind::Borrow(_, _) => 64, TypeKind::Borrow(_, _) => 64,
TypeKind::UserPtr(_) => 64,
} }
} }
@ -397,8 +399,8 @@ impl Collapsable for TypeKind {
)) ))
} }
} }
(TypeKind::Ptr(val1), TypeKind::Ptr(val2)) => { (TypeKind::UserPtr(val1), TypeKind::UserPtr(val2)) => {
Ok(TypeKind::Ptr(Box::new(val1.collapse_into(val2)?))) Ok(TypeKind::UserPtr(Box::new(val1.collapse_into(val2)?)))
} }
_ => Err(ErrorKind::TypesIncompatible(self.clone(), other.clone())), _ => Err(ErrorKind::TypesIncompatible(self.clone(), other.clone())),
} }

View File

@ -95,7 +95,8 @@ pub enum TypeKind {
Array(Box<TypeKind>, u64), Array(Box<TypeKind>, u64),
CustomType(String), CustomType(String),
Borrow(Box<TypeKind>, bool), Borrow(Box<TypeKind>, bool),
Ptr(Box<TypeKind>), UserPtr(Box<TypeKind>),
CodegenPtr(Box<TypeKind>),
Vague(VagueType), Vague(VagueType),
} }

View File

@ -39,8 +39,8 @@ pub enum ErrorKind {
TypeNotInferrable(TypeKind), TypeNotInferrable(TypeKind),
#[error("Expected branch type to be {0}, found {1} instead")] #[error("Expected branch type to be {0}, found {1} instead")]
BranchTypesDiffer(TypeKind, TypeKind), BranchTypesDiffer(TypeKind, TypeKind),
#[error("Attempted to index a non-array type of {0}")] #[error("Attempted to index a non-indexable type of {0}")]
TriedIndexingNonArray(TypeKind), TriedIndexingNonIndexable(TypeKind),
#[error("Index {0} out of bounds ({1})")] #[error("Index {0} out of bounds ({1})")]
IndexOutOfBounds(u64, u64), IndexOutOfBounds(u64, u64),
#[error("No such type {0} could be found")] #[error("No such type {0} could be found")]
@ -535,7 +535,7 @@ impl Expression {
let expr_t = expression.typecheck(state, typerefs, hint_t)?; let expr_t = expression.typecheck(state, typerefs, hint_t)?;
match expr_t { match expr_t {
TypeKind::Array(inferred_ty, _) | TypeKind::Ptr(inferred_ty) => { TypeKind::Array(inferred_ty, _) | TypeKind::UserPtr(inferred_ty) => {
let ty = state.or_else( let ty = state.or_else(
elem_ty.resolve_ref(typerefs).collapse_into(&inferred_ty), elem_ty.resolve_ref(typerefs).collapse_into(&inferred_ty),
TypeKind::Vague(Vague::Unknown), TypeKind::Vague(Vague::Unknown),
@ -544,7 +544,7 @@ impl Expression {
*elem_ty = ty.clone(); *elem_ty = ty.clone();
Ok(ty) Ok(ty)
} }
_ => Err(ErrorKind::TriedIndexingNonArray(expr_t)), _ => Err(ErrorKind::TriedIndexingNonIndexable(expr_t)),
} }
} }
ExprKind::Array(expressions) => { ExprKind::Array(expressions) => {

View File

@ -267,12 +267,12 @@ impl Expression {
// need for further resolution. // need for further resolution.
let kind = expr_ty.resolve_weak().unwrap(); let kind = expr_ty.resolve_weak().unwrap();
match kind { match kind {
Array(type_kind, _) | Ptr(type_kind) => { Array(type_kind, _) | UserPtr(type_kind) => {
let elem_ty = type_refs.from_type(&type_kind).unwrap(); let elem_ty = type_refs.from_type(&type_kind).unwrap();
*index_ty = elem_ty.as_type().clone(); *index_ty = elem_ty.as_type().clone();
Ok(elem_ty) Ok(elem_ty)
} }
_ => Err(ErrorKind::TriedIndexingNonArray(kind)), _ => Err(ErrorKind::TriedIndexingNonIndexable(kind)),
} }
} }
ExprKind::Array(expressions) => { ExprKind::Array(expressions) => {