From d0796aa5df3f8126e5e0b118d8f439349cfd29e2 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sun, 7 Jun 2026 00:30:27 +0200 Subject: [PATCH] Working param and return type fn type --- crates/codegen/src/class_decl.rs | 19 ++++++++++++++--- crates/codegen/src/lib.rs | 35 ++++++++++++++++++++++++-------- crates/parser/src/ast/mod.rs | 6 +++++- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/crates/codegen/src/class_decl.rs b/crates/codegen/src/class_decl.rs index 6a63a87..3008614 100644 --- a/crates/codegen/src/class_decl.rs +++ b/crates/codegen/src/class_decl.rs @@ -317,13 +317,26 @@ pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustC param_types.remove(0); - // param_types.insert(0, ); + param_types.insert( + 0, + TypeExpr::UnsafePtr { + mutable: true, + ty: Box::new(TypeExpr::Path( + Path(vec![ + Identifier(String::from("std")), + Identifier(String::from("ffi")), + Identifier(String::from("c_void")), + ]), + None, + )), + }, + ); - cg.add(&TypeExpr::StaticFn(param_types).get_rust()); + cg.add(&TypeExpr::StaticFn(param_types, method.return_type.clone().map(Box::new)).get_rust()); cg.addln(" = std::mem::transmute(func_ptr);"); - cg.add_indentedln("func(self._m_oop.1);"); + cg.add_indentedln("func(self._m_oop.1)"); cg.indent -= 1; cg.add_indentedln("}"); diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index b0d79de..ea63d97 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -192,14 +192,33 @@ impl GetRust for TypeExpr { .collect::>() .join(", ") ), - Self::StaticFn(types) => format!( - "fn({})", - types - .into_iter() - .map(|t| t.get_rust()) - .collect::>() - .join(", ") - ), + Self::StaticFn(types, return_type) => { + if let Some(return_type) = return_type { + format!( + "fn({}) -> {}", + types + .into_iter() + .map(|t| t.get_rust()) + .collect::>() + .join(", "), + return_type.get_rust() + ) + } else { + format!( + "fn({})", + types + .into_iter() + .map(|t| t.get_rust()) + .collect::>() + .join(", "), + ) + } + } + + Self::UnsafePtr { mutable, ty } => { + let mutable = if *mutable { "mut " } else { "const " }; + format!("*{mutable}{}", ty.get_rust()) + } Self::Ref { lifetime, diff --git a/crates/parser/src/ast/mod.rs b/crates/parser/src/ast/mod.rs index 11014a3..89b12fb 100644 --- a/crates/parser/src/ast/mod.rs +++ b/crates/parser/src/ast/mod.rs @@ -24,9 +24,13 @@ pub enum TypeExpr { mutable: bool, ty: Box, }, + UnsafePtr { + mutable: bool, + ty: Box, + }, Dyn(Box), Path(Path, Option), - StaticFn(Vec), + StaticFn(Vec, Option>), Tuple(Vec), Lifetime(Identifier), }