Add generic typedef

This commit is contained in:
Sofia 2025-08-25 19:18:25 +03:00
parent 13e462def7
commit 91fd0d4d5a
10 changed files with 28 additions and 6 deletions

View File

@ -216,6 +216,7 @@ impl mir::Module {
} }
} }
} }
TypeDefinitionKind::Generic => panic!("Tried compiling a generic!"),
}; };
if is_ok { if is_ok {
@ -240,6 +241,7 @@ impl mir::Module {
.collect(), .collect(),
))) )))
} }
TypeDefinitionKind::Generic => panic!("Tried compiling a generic!"),
}; };
types.insert(type_value, typedef.clone()); types.insert(type_value, typedef.clone());
type_values.insert(type_key.clone(), type_value); type_values.insert(type_key.clone(), type_value);
@ -1218,7 +1220,9 @@ impl mir::Expression {
let TypeKind::CustomType(key) = *inner.clone() else { let TypeKind::CustomType(key) = *inner.clone() else {
panic!("tried accessing non-custom-type"); panic!("tried accessing non-custom-type");
}; };
let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&key).unwrap().kind.clone(); let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&key).unwrap().kind.clone() else {
panic!();
};
let idx = struct_ty.find_index(field).unwrap(); let idx = struct_ty.find_index(field).unwrap();
let gep_n = format!("{}.{}.gep", key.0, field); let gep_n = format!("{}.{}.gep", key.0, field);
@ -1259,7 +1263,10 @@ impl mir::Expression {
let TypeDefinition { let TypeDefinition {
kind: TypeDefinitionKind::Struct(struct_ty), kind: TypeDefinitionKind::Struct(struct_ty),
.. ..
} = scope.types.get(scope.type_values.get(&key).unwrap()).unwrap(); } = scope.types.get(scope.type_values.get(&key).unwrap()).unwrap()
else {
panic!()
};
let indices = struct_ty.0.iter().enumerate(); let indices = struct_ty.0.iter().enumerate();

View File

@ -211,6 +211,7 @@ impl TypeKind {
}) })
} }
} }
TypeDefinitionKind::Generic => panic!("Tried to generate debug-info for a generic!"),
} }
} }
_ => DebugTypeData::Basic(DebugBasicType { _ => DebugTypeData::Basic(DebugBasicType {

View File

@ -140,6 +140,7 @@ impl Display for TypeDefinitionKind {
} }
f.write_char('}') f.write_char('}')
} }
TypeDefinitionKind::Generic => write!(f, "generic"),
} }
} }
} }
@ -490,12 +491,14 @@ impl Display for VagueType {
VagueType::Integer => write!(f, "Number"), VagueType::Integer => write!(f, "Number"),
VagueType::TypeRef(id) => write!(f, "TypeRef({0})", id), VagueType::TypeRef(id) => write!(f, "TypeRef({0})", id),
VagueType::Decimal => write!(f, "Decimal"), VagueType::Decimal => write!(f, "Decimal"),
VagueType::Named(name) => write!(f, "Named<{name}>"),
} }
} else { } else {
match self { match self {
VagueType::Unknown => write!(f, "{{unknown}}"), VagueType::Unknown => write!(f, "{{unknown}}"),
VagueType::Integer => write!(f, "Number"), VagueType::Integer => write!(f, "Number"),
VagueType::TypeRef(_) => write!(f, "{{unknown}}"), VagueType::TypeRef(_) => write!(f, "{{unknown}}"),
VagueType::Named(name) => write!(f, "{name}"),
VagueType::Decimal => write!(f, "Decimal"), VagueType::Decimal => write!(f, "Decimal"),
} }
} }

View File

@ -83,6 +83,7 @@ impl TypeKind {
} }
size size
} }
TypeDefinitionKind::Generic => 404,
}, },
// Easy to recognize default number. Used e.g. when sorting // Easy to recognize default number. Used e.g. when sorting
// types by size // types by size
@ -174,6 +175,7 @@ impl TypeKind {
VagueType::Integer => TypeCategory::Integer, VagueType::Integer => TypeCategory::Integer,
VagueType::Decimal => TypeCategory::Real, VagueType::Decimal => TypeCategory::Real,
VagueType::TypeRef(_) => TypeCategory::TypeRef, VagueType::TypeRef(_) => TypeCategory::TypeRef,
VagueType::Named(_) => TypeCategory::Other,
}, },
TypeKind::Generic(_) => TypeCategory::Other, TypeKind::Generic(_) => TypeCategory::Other,
} }

View File

@ -542,6 +542,7 @@ impl<'map> Pass for LinkerPass<'map> {
field.1 = field.1.update_imported(foreign_types); field.1 = field.1.update_imported(foreign_types);
} }
} }
TypeDefinitionKind::Generic => {}
} }
} }
} }
@ -702,6 +703,7 @@ fn resolve_types_recursively(
types.extend(resolve_types_recursively(&field.1, modules, seen.clone())?); types.extend(resolve_types_recursively(&field.1, modules, seen.clone())?);
} }
} }
TypeDefinitionKind::Generic => {}
} }
} }
TypeKind::Array(type_kind, _) => types.extend(resolve_types_recursively(&type_kind, modules, seen.clone())?), TypeKind::Array(type_kind, _) => types.extend(resolve_types_recursively(&type_kind, modules, seen.clone())?),

View File

@ -136,13 +136,14 @@ pub enum TypeKind {
Vague(VagueType), Vague(VagueType),
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum VagueType { pub enum VagueType {
Unknown, Unknown,
/// Some integer value (e.g. 5) /// Some integer value (e.g. 5)
Integer, Integer,
/// Some decimal fractional value (e.g. 1.5) /// Some decimal fractional value (e.g. 1.5)
Decimal, Decimal,
Named(String),
TypeRef(usize), TypeRef(usize),
} }
@ -165,7 +166,7 @@ impl StructType {
impl TypeKind { impl TypeKind {
pub fn known(&self) -> Result<TypeKind, VagueType> { pub fn known(&self) -> Result<TypeKind, VagueType> {
if let TypeKind::Vague(vague) = self { if let TypeKind::Vague(vague) = self {
Err(*vague) Err(vague.clone())
} else { } else {
Ok(self.clone()) Ok(self.clone())
} }
@ -421,6 +422,7 @@ pub struct TypeDefinition {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum TypeDefinitionKind { pub enum TypeDefinitionKind {
Struct(StructType), Struct(StructType),
Generic,
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -150,6 +150,7 @@ impl<Data: Clone + Default> Scope<Data> {
let ty = self.types.get(&key)?; let ty = self.types.get(&key)?;
match &ty.kind { match &ty.kind {
TypeDefinitionKind::Struct(struct_ty) => Some(struct_ty), TypeDefinitionKind::Struct(struct_ty) => Some(struct_ty),
TypeDefinitionKind::Generic => None,
} }
} }

View File

@ -290,10 +290,11 @@ impl TypeKind {
pub(super) fn or_default(&self) -> Result<TypeKind, ErrorKind> { pub(super) fn or_default(&self) -> Result<TypeKind, ErrorKind> {
Ok(match self { Ok(match self {
TypeKind::Vague(vague_type) => match &vague_type { TypeKind::Vague(vague_type) => match &vague_type {
Vague::Unknown => Err(ErrorKind::TypeIsVague(*vague_type))?, Vague::Unknown => Err(ErrorKind::TypeIsVague(vague_type.clone()))?,
Vague::Integer => TypeKind::I32, Vague::Integer => TypeKind::I32,
Vague::TypeRef(_) => panic!("Hinted default!"), Vague::TypeRef(_) => panic!("Hinted default!"),
VagueType::Decimal => TypeKind::F32, VagueType::Decimal => TypeKind::F32,
VagueType::Named(_) => panic!("Defaulted unknown named!"),
}, },
TypeKind::Array(type_kind, len) => TypeKind::Array(Box::new(type_kind.or_default()?), *len), TypeKind::Array(type_kind, len) => TypeKind::Array(Box::new(type_kind.or_default()?), *len),
TypeKind::Borrow(type_kind, mutable) => TypeKind::Borrow(Box::new(type_kind.or_default()?), *mutable), TypeKind::Borrow(type_kind, mutable) => TypeKind::Borrow(Box::new(type_kind.or_default()?), *mutable),
@ -339,7 +340,7 @@ impl TypeKind {
TypeKind::Borrow(type_kind, _) => type_kind.is_known(state), TypeKind::Borrow(type_kind, _) => type_kind.is_known(state),
TypeKind::UserPtr(type_kind) => type_kind.is_known(state), TypeKind::UserPtr(type_kind) => type_kind.is_known(state),
TypeKind::CodegenPtr(type_kind) => type_kind.is_known(state), TypeKind::CodegenPtr(type_kind) => type_kind.is_known(state),
TypeKind::Vague(vague_type) => Err(ErrorKind::TypeIsVague(*vague_type)), TypeKind::Vague(vague_type) => Err(ErrorKind::TypeIsVague(vague_type.clone())),
_ => Ok(()), _ => Ok(()),
} }
} }

View File

@ -48,6 +48,7 @@ impl<'t> Pass for TypeCheck<'t> {
} }
} }
} }
TypeDefinitionKind::Generic => todo!(),
} }
if typedef.source_module == module.module_id || typedef.importer == Some(module.module_id) { if typedef.source_module == module.module_id || typedef.importer == Some(module.module_id) {
@ -106,6 +107,7 @@ fn check_typedefs_for_recursion<'a, 'b>(
} }
} }
} }
TypeDefinitionKind::Generic => {}
} }
} }

View File

@ -368,6 +368,7 @@ impl<'outer> ScopeTypeRefs<'outer> {
self.narrow_to_type(&typeref, &self.try_default_deep(&typeref.resolve_deep()?)?)? self.narrow_to_type(&typeref, &self.try_default_deep(&typeref.resolve_deep()?)?)?
.resolve_deep()? .resolve_deep()?
} }
VagueType::Named(_) => ty.clone(),
}, },
_ => ty.clone(), _ => ty.clone(),
}) })