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 {
@ -240,6 +241,7 @@ impl mir::Module {
.collect(),
)))
}
TypeDefinitionKind::Generic => panic!("Tried compiling a generic!"),
};
types.insert(type_value, typedef.clone());
type_values.insert(type_key.clone(), type_value);
@ -1218,7 +1220,9 @@ impl mir::Expression {
let TypeKind::CustomType(key) = *inner.clone() else {
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 gep_n = format!("{}.{}.gep", key.0, field);
@ -1259,7 +1263,10 @@ impl mir::Expression {
let TypeDefinition {
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();

View File

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

View File

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

View File

@ -83,6 +83,7 @@ impl TypeKind {
}
size
}
TypeDefinitionKind::Generic => 404,
},
// Easy to recognize default number. Used e.g. when sorting
// types by size
@ -174,6 +175,7 @@ impl TypeKind {
VagueType::Integer => TypeCategory::Integer,
VagueType::Decimal => TypeCategory::Real,
VagueType::TypeRef(_) => TypeCategory::TypeRef,
VagueType::Named(_) => 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);
}
}
TypeDefinitionKind::Generic => {}
}
}
}
@ -702,6 +703,7 @@ fn resolve_types_recursively(
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())?),

View File

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

View File

@ -150,6 +150,7 @@ impl<Data: Clone + Default> Scope<Data> {
let ty = self.types.get(&key)?;
match &ty.kind {
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> {
Ok(match self {
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::TypeRef(_) => panic!("Hinted default!"),
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::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::UserPtr(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(()),
}
}

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) {
@ -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()?)?)?
.resolve_deep()?
}
VagueType::Named(_) => ty.clone(),
},
_ => ty.clone(),
})