From ff83d7b4f0c8c62a6eef4e8e072e499d6f041f12 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 25 Aug 2025 18:46:29 +0300 Subject: [PATCH] Add generics to MIR data structures --- reid/src/ast/process.rs | 12 +++++++++++- reid/src/codegen/intrinsics.rs | 10 ++++++++++ reid/src/codegen/util.rs | 1 + reid/src/mir/fmt.rs | 1 + reid/src/mir/implement.rs | 4 ++++ reid/src/mir/linker.rs | 2 ++ reid/src/mir/mod.rs | 3 +++ 7 files changed, 32 insertions(+), 1 deletion(-) diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 59178e2..77a8fae 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::{collections::HashMap, path::PathBuf}; use crate::{ ast::{self, ReturnType}, @@ -44,6 +44,8 @@ impl ast::Module { let def = mir::FunctionDefinition { name: signature.name.clone(), documentation: signature.documentation.clone(), + // TODO generics parsing + generics: Vec::new(), linkage_name: None, is_pub: false, is_imported: false, @@ -178,6 +180,8 @@ impl ast::FunctionDefinition { mir::FunctionDefinition { name: signature.name.clone(), documentation: signature.documentation.clone(), + // TODO generics parsing + generics: Vec::new(), linkage_name: None, is_pub: *is_pub, is_imported: false, @@ -379,6 +383,8 @@ impl ast::Expression { ), ast::ExpressionKind::FunctionCall(fn_call_expr) => mir::ExprKind::FunctionCall(mir::FunctionCall { name: fn_call_expr.name.clone(), + // TODO generics parsing + generics: Vec::new(), return_type: mir::TypeKind::Vague(mir::VagueType::Unknown), parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(), meta: fn_call_expr.range.as_meta(module_id), @@ -468,6 +474,8 @@ impl ast::Expression { ty.0.into_mir(module_id), mir::FunctionCall { name: fn_call_expr.name.clone(), + // TODO generics parsing + generics: Vec::new(), return_type: mir::TypeKind::Vague(mir::VagueType::Unknown), parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(), meta: fn_call_expr.range.as_meta(module_id), @@ -491,6 +499,8 @@ impl ast::Expression { mir::TypeKind::Vague(mir::VagueType::Unknown), mir::FunctionCall { name: fn_call_expr.name.clone(), + // TODO generics parsing + generics: Vec::new(), return_type: mir::TypeKind::Vague(mir::VagueType::Unknown), parameters: params, meta: fn_call_expr.range.as_meta(module_id), diff --git a/reid/src/codegen/intrinsics.rs b/reid/src/codegen/intrinsics.rs index 3a06f20..af8d4fa 100644 --- a/reid/src/codegen/intrinsics.rs +++ b/reid/src/codegen/intrinsics.rs @@ -77,6 +77,7 @@ pub fn form_intrinsics() -> Vec { intrinsics.push(FunctionDefinition { name: MALLOC_IDENT.to_owned(), + generics: Vec::new(), documentation: doc!("Allocates `size` bytes and returns a `u8`-pointer."), linkage_name: Some("malloc".to_owned()), is_pub: false, @@ -104,6 +105,7 @@ pub fn simple_intrinsic + Clone>( ) -> FunctionDefinition { FunctionDefinition { name: name.into(), + generics: Vec::new(), documentation: Some(doc.into()), linkage_name: None, is_pub: true, @@ -124,6 +126,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec { if let TypeKind::Array(_, len) = ty { intrinsics.push(FunctionDefinition { name: "length".to_owned(), + generics: Vec::new(), documentation: doc!("Returns the length of this given array"), linkage_name: None, is_pub: true, @@ -282,6 +285,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec { )); intrinsics.push(FunctionDefinition { name: "powi".to_owned(), + generics: Vec::new(), documentation: doc!("Returns `value` raised to the exponent of `exponent`."), linkage_name: None, is_pub: true, @@ -326,6 +330,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec { if ty.signed() { intrinsics.push(FunctionDefinition { name: "abs".to_owned(), + generics: Vec::new(), documentation: doc!("Returns the absolute value of `value`."), linkage_name: None, is_pub: true, @@ -357,6 +362,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec { } intrinsics.push(FunctionDefinition { name: "sizeof".to_owned(), + generics: Vec::new(), documentation: doc!("Simply returns the size of type `T` in bytes."), linkage_name: None, is_pub: true, @@ -369,6 +375,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec { }); intrinsics.push(FunctionDefinition { name: "malloc".to_owned(), + generics: Vec::new(), documentation: doc!("Allocates `T::sizeof() * size` bytes and returns a pointer to `T`."), linkage_name: None, is_pub: true, @@ -386,6 +393,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec { intrinsics.push(FunctionDefinition { name: "memcpy".to_owned(), + generics: Vec::new(), documentation: doc!( "Copies `T::sizeof() * size` bytes from pointer `source` to pointer `destination`." @@ -418,6 +426,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec { intrinsics.push(FunctionDefinition { name: "null".to_owned(), + generics: Vec::new(), documentation: doc!("Returns a null-pointer of type `T`."), linkage_name: None, is_pub: true, @@ -431,6 +440,7 @@ pub fn get_intrinsic_assoc_functions(ty: &TypeKind) -> Vec { intrinsics.push(FunctionDefinition { name: "is_null".to_owned(), + generics: Vec::new(), documentation: doc!("Returns a boolean representing if `val` is a nullptr or not."), linkage_name: None, is_pub: true, diff --git a/reid/src/codegen/util.rs b/reid/src/codegen/util.rs index d3200e3..1f903b5 100644 --- a/reid/src/codegen/util.rs +++ b/reid/src/codegen/util.rs @@ -96,6 +96,7 @@ impl TypeKind { 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::Borrow(type_kind, _) => Type::Ptr(Box::new(type_kind.get_type(type_vals))), + TypeKind::Generic(_) => panic!("Tried to compile a generic type!"), } } } diff --git a/reid/src/mir/fmt.rs b/reid/src/mir/fmt.rs index bd23860..faf775e 100644 --- a/reid/src/mir/fmt.rs +++ b/reid/src/mir/fmt.rs @@ -477,6 +477,7 @@ impl Display for TypeKind { TypeKind::F128 => write!(f, "f128"), TypeKind::F80 => write!(f, "f80"), TypeKind::F128PPC => write!(f, "f128ppc"), + TypeKind::Generic(name) => write!(f, "Generic<{}>", name), } } } diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index d44ab80..37771dd 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -54,6 +54,7 @@ impl TypeKind { TypeKind::F128 => true, TypeKind::F80 => true, TypeKind::F128PPC => true, + TypeKind::Generic(_) => false, } } @@ -98,6 +99,7 @@ impl TypeKind { TypeKind::F128 => 128, TypeKind::F80 => 80, TypeKind::F128PPC => 128, + TypeKind::Generic(_) => 0, } } @@ -129,6 +131,7 @@ impl TypeKind { TypeKind::F128 => 128, TypeKind::F80 => 80, TypeKind::F128PPC => 128, + TypeKind::Generic(_) => 0, } } @@ -172,6 +175,7 @@ impl TypeKind { VagueType::Decimal => TypeCategory::Real, VagueType::TypeRef(_) => TypeCategory::TypeRef, }, + TypeKind::Generic(_) => TypeCategory::Other, } } diff --git a/reid/src/mir/linker.rs b/reid/src/mir/linker.rs index eda2aff..606d4da 100644 --- a/reid/src/mir/linker.rs +++ b/reid/src/mir/linker.rs @@ -311,6 +311,7 @@ impl<'map> Pass for LinkerPass<'map> { importer_module.functions.push(FunctionDefinition { name: function.name.clone(), + generics: function.generics.clone(), documentation: function.documentation.clone(), linkage_name: None, is_pub: false, @@ -460,6 +461,7 @@ impl<'map> Pass for LinkerPass<'map> { FunctionDefinition { name: func_name.clone(), documentation: func.documentation.clone(), + generics: func.generics.clone(), linkage_name: Some(format!("{}::{}", ty, func_name)), is_pub: false, is_imported: false, diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 3713bae..78d9136 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -132,6 +132,7 @@ pub enum TypeKind { Borrow(Box, bool), UserPtr(Box), CodegenPtr(Box), + Generic(String), Vague(VagueType), } @@ -309,6 +310,7 @@ pub struct IfExpression(pub Box, pub Box, pub Box