Add generics to MIR data structures

This commit is contained in:
Sofia 2025-08-25 18:46:29 +03:00
parent 663f327ccf
commit ff83d7b4f0
7 changed files with 32 additions and 1 deletions

View File

@ -1,4 +1,4 @@
use std::path::PathBuf; use std::{collections::HashMap, path::PathBuf};
use crate::{ use crate::{
ast::{self, ReturnType}, ast::{self, ReturnType},
@ -44,6 +44,8 @@ impl ast::Module {
let def = mir::FunctionDefinition { let def = mir::FunctionDefinition {
name: signature.name.clone(), name: signature.name.clone(),
documentation: signature.documentation.clone(), documentation: signature.documentation.clone(),
// TODO generics parsing
generics: Vec::new(),
linkage_name: None, linkage_name: None,
is_pub: false, is_pub: false,
is_imported: false, is_imported: false,
@ -178,6 +180,8 @@ impl ast::FunctionDefinition {
mir::FunctionDefinition { mir::FunctionDefinition {
name: signature.name.clone(), name: signature.name.clone(),
documentation: signature.documentation.clone(), documentation: signature.documentation.clone(),
// TODO generics parsing
generics: Vec::new(),
linkage_name: None, linkage_name: None,
is_pub: *is_pub, is_pub: *is_pub,
is_imported: false, is_imported: false,
@ -379,6 +383,8 @@ impl ast::Expression {
), ),
ast::ExpressionKind::FunctionCall(fn_call_expr) => mir::ExprKind::FunctionCall(mir::FunctionCall { ast::ExpressionKind::FunctionCall(fn_call_expr) => mir::ExprKind::FunctionCall(mir::FunctionCall {
name: fn_call_expr.name.clone(), name: fn_call_expr.name.clone(),
// TODO generics parsing
generics: Vec::new(),
return_type: mir::TypeKind::Vague(mir::VagueType::Unknown), return_type: mir::TypeKind::Vague(mir::VagueType::Unknown),
parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(), parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(),
meta: fn_call_expr.range.as_meta(module_id), meta: fn_call_expr.range.as_meta(module_id),
@ -468,6 +474,8 @@ impl ast::Expression {
ty.0.into_mir(module_id), ty.0.into_mir(module_id),
mir::FunctionCall { mir::FunctionCall {
name: fn_call_expr.name.clone(), name: fn_call_expr.name.clone(),
// TODO generics parsing
generics: Vec::new(),
return_type: mir::TypeKind::Vague(mir::VagueType::Unknown), return_type: mir::TypeKind::Vague(mir::VagueType::Unknown),
parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(), parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(),
meta: fn_call_expr.range.as_meta(module_id), meta: fn_call_expr.range.as_meta(module_id),
@ -491,6 +499,8 @@ impl ast::Expression {
mir::TypeKind::Vague(mir::VagueType::Unknown), mir::TypeKind::Vague(mir::VagueType::Unknown),
mir::FunctionCall { mir::FunctionCall {
name: fn_call_expr.name.clone(), name: fn_call_expr.name.clone(),
// TODO generics parsing
generics: Vec::new(),
return_type: mir::TypeKind::Vague(mir::VagueType::Unknown), return_type: mir::TypeKind::Vague(mir::VagueType::Unknown),
parameters: params, parameters: params,
meta: fn_call_expr.range.as_meta(module_id), meta: fn_call_expr.range.as_meta(module_id),

View File

@ -77,6 +77,7 @@ pub fn form_intrinsics() -> Vec<FunctionDefinition> {
intrinsics.push(FunctionDefinition { intrinsics.push(FunctionDefinition {
name: MALLOC_IDENT.to_owned(), name: MALLOC_IDENT.to_owned(),
generics: Vec::new(),
documentation: doc!("Allocates `size` bytes and returns a `u8`-pointer."), documentation: doc!("Allocates `size` bytes and returns a `u8`-pointer."),
linkage_name: Some("malloc".to_owned()), linkage_name: Some("malloc".to_owned()),
is_pub: false, is_pub: false,
@ -104,6 +105,7 @@ pub fn simple_intrinsic<T: Into<String> + Clone>(
) -> FunctionDefinition { ) -> FunctionDefinition {
FunctionDefinition { FunctionDefinition {
name: name.into(), name: name.into(),
generics: Vec::new(),
documentation: Some(doc.into()), documentation: Some(doc.into()),
linkage_name: None, linkage_name: None,
is_pub: true, is_pub: true,
@ -124,6 +126,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
if let TypeKind::Array(_, len) = ty { if let TypeKind::Array(_, len) = ty {
intrinsics.push(FunctionDefinition { intrinsics.push(FunctionDefinition {
name: "length".to_owned(), name: "length".to_owned(),
generics: Vec::new(),
documentation: doc!("Returns the length of this given array"), documentation: doc!("Returns the length of this given array"),
linkage_name: None, linkage_name: None,
is_pub: true, is_pub: true,
@ -282,6 +285,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
)); ));
intrinsics.push(FunctionDefinition { intrinsics.push(FunctionDefinition {
name: "powi".to_owned(), name: "powi".to_owned(),
generics: Vec::new(),
documentation: doc!("Returns `value` raised to the exponent of `exponent`."), documentation: doc!("Returns `value` raised to the exponent of `exponent`."),
linkage_name: None, linkage_name: None,
is_pub: true, is_pub: true,
@ -326,6 +330,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
if ty.signed() { if ty.signed() {
intrinsics.push(FunctionDefinition { intrinsics.push(FunctionDefinition {
name: "abs".to_owned(), name: "abs".to_owned(),
generics: Vec::new(),
documentation: doc!("Returns the absolute value of `value`."), documentation: doc!("Returns the absolute value of `value`."),
linkage_name: None, linkage_name: None,
is_pub: true, is_pub: true,
@ -357,6 +362,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
} }
intrinsics.push(FunctionDefinition { intrinsics.push(FunctionDefinition {
name: "sizeof".to_owned(), name: "sizeof".to_owned(),
generics: Vec::new(),
documentation: doc!("Simply returns the size of type `T` in bytes."), documentation: doc!("Simply returns the size of type `T` in bytes."),
linkage_name: None, linkage_name: None,
is_pub: true, is_pub: true,
@ -369,6 +375,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
}); });
intrinsics.push(FunctionDefinition { intrinsics.push(FunctionDefinition {
name: "malloc".to_owned(), name: "malloc".to_owned(),
generics: Vec::new(),
documentation: doc!("Allocates `T::sizeof() * size` bytes and returns a pointer to `T`."), documentation: doc!("Allocates `T::sizeof() * size` bytes and returns a pointer to `T`."),
linkage_name: None, linkage_name: None,
is_pub: true, is_pub: true,
@ -386,6 +393,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
intrinsics.push(FunctionDefinition { intrinsics.push(FunctionDefinition {
name: "memcpy".to_owned(), name: "memcpy".to_owned(),
generics: Vec::new(),
documentation: doc!( documentation: doc!(
"Copies `T::sizeof() * size` bytes from pointer `source` to pointer "Copies `T::sizeof() * size` bytes from pointer `source` to pointer
`destination`." `destination`."
@ -418,6 +426,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
intrinsics.push(FunctionDefinition { intrinsics.push(FunctionDefinition {
name: "null".to_owned(), name: "null".to_owned(),
generics: Vec::new(),
documentation: doc!("Returns a null-pointer of type `T`."), documentation: doc!("Returns a null-pointer of type `T`."),
linkage_name: None, linkage_name: None,
is_pub: true, is_pub: true,
@ -431,6 +440,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec<FunctionDefinition> {
intrinsics.push(FunctionDefinition { intrinsics.push(FunctionDefinition {
name: "is_null".to_owned(), name: "is_null".to_owned(),
generics: Vec::new(),
documentation: doc!("Returns a boolean representing if `val` is a nullptr or not."), documentation: doc!("Returns a boolean representing if `val` is a nullptr or not."),
linkage_name: None, linkage_name: None,
is_pub: true, is_pub: true,

View File

@ -96,6 +96,7 @@ impl TypeKind {
TypeKind::UserPtr(type_kind) => Type::Ptr(Box::new(type_kind.get_type(type_vals))), TypeKind::UserPtr(type_kind) => Type::Ptr(Box::new(type_kind.get_type(type_vals))),
TypeKind::CodegenPtr(type_kind) => Type::Ptr(Box::new(type_kind.get_type(type_vals))), TypeKind::CodegenPtr(type_kind) => Type::Ptr(Box::new(type_kind.get_type(type_vals))),
TypeKind::Borrow(type_kind, _) => Type::Ptr(Box::new(type_kind.get_type(type_vals))), TypeKind::Borrow(type_kind, _) => Type::Ptr(Box::new(type_kind.get_type(type_vals))),
TypeKind::Generic(_) => panic!("Tried to compile a generic type!"),
} }
} }
} }

View File

@ -477,6 +477,7 @@ impl Display for TypeKind {
TypeKind::F128 => write!(f, "f128"), TypeKind::F128 => write!(f, "f128"),
TypeKind::F80 => write!(f, "f80"), TypeKind::F80 => write!(f, "f80"),
TypeKind::F128PPC => write!(f, "f128ppc"), TypeKind::F128PPC => write!(f, "f128ppc"),
TypeKind::Generic(name) => write!(f, "Generic<{}>", name),
} }
} }
} }

View File

@ -54,6 +54,7 @@ impl TypeKind {
TypeKind::F128 => true, TypeKind::F128 => true,
TypeKind::F80 => true, TypeKind::F80 => true,
TypeKind::F128PPC => true, TypeKind::F128PPC => true,
TypeKind::Generic(_) => false,
} }
} }
@ -98,6 +99,7 @@ impl TypeKind {
TypeKind::F128 => 128, TypeKind::F128 => 128,
TypeKind::F80 => 80, TypeKind::F80 => 80,
TypeKind::F128PPC => 128, TypeKind::F128PPC => 128,
TypeKind::Generic(_) => 0,
} }
} }
@ -129,6 +131,7 @@ impl TypeKind {
TypeKind::F128 => 128, TypeKind::F128 => 128,
TypeKind::F80 => 80, TypeKind::F80 => 80,
TypeKind::F128PPC => 128, TypeKind::F128PPC => 128,
TypeKind::Generic(_) => 0,
} }
} }
@ -172,6 +175,7 @@ impl TypeKind {
VagueType::Decimal => TypeCategory::Real, VagueType::Decimal => TypeCategory::Real,
VagueType::TypeRef(_) => TypeCategory::TypeRef, VagueType::TypeRef(_) => TypeCategory::TypeRef,
}, },
TypeKind::Generic(_) => TypeCategory::Other,
} }
} }

View File

@ -311,6 +311,7 @@ impl<'map> Pass for LinkerPass<'map> {
importer_module.functions.push(FunctionDefinition { importer_module.functions.push(FunctionDefinition {
name: function.name.clone(), name: function.name.clone(),
generics: function.generics.clone(),
documentation: function.documentation.clone(), documentation: function.documentation.clone(),
linkage_name: None, linkage_name: None,
is_pub: false, is_pub: false,
@ -460,6 +461,7 @@ impl<'map> Pass for LinkerPass<'map> {
FunctionDefinition { FunctionDefinition {
name: func_name.clone(), name: func_name.clone(),
documentation: func.documentation.clone(), documentation: func.documentation.clone(),
generics: func.generics.clone(),
linkage_name: Some(format!("{}::{}", ty, func_name)), linkage_name: Some(format!("{}::{}", ty, func_name)),
is_pub: false, is_pub: false,
is_imported: false, is_imported: false,

View File

@ -132,6 +132,7 @@ pub enum TypeKind {
Borrow(Box<TypeKind>, bool), Borrow(Box<TypeKind>, bool),
UserPtr(Box<TypeKind>), UserPtr(Box<TypeKind>),
CodegenPtr(Box<TypeKind>), CodegenPtr(Box<TypeKind>),
Generic(String),
Vague(VagueType), Vague(VagueType),
} }
@ -309,6 +310,7 @@ pub struct IfExpression(pub Box<Expression>, pub Box<Expression>, pub Box<Option
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FunctionCall { pub struct FunctionCall {
pub name: String, pub name: String,
pub generics: Vec<TypeKind>,
pub return_type: TypeKind, pub return_type: TypeKind,
pub parameters: Vec<Expression>, pub parameters: Vec<Expression>,
pub is_macro: bool, pub is_macro: bool,
@ -320,6 +322,7 @@ pub struct FunctionDefinition {
pub name: String, pub name: String,
pub documentation: Option<String>, pub documentation: Option<String>,
pub linkage_name: Option<String>, pub linkage_name: Option<String>,
pub generics: Vec<String>,
/// Whether this function is visible to outside modules /// Whether this function is visible to outside modules
pub is_pub: bool, pub is_pub: bool,
/// Whether this module is from an external module, and has been imported /// Whether this module is from an external module, and has been imported