From a9f6cce1b3825612e8f1e69af8e3fbfddd4bee94 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 9 Jun 2026 13:51:32 +0200 Subject: [PATCH] Improved override --- crates/codegen/src/class_decl.rs | 38 +++++++++++---------------- crates/codegen/src/expr.rs | 12 +-------- crates/codegen/src/lib.rs | 2 +- crates/parser/src/ast/top_level.rs | 2 +- crates/parser/src/grammar.pest | 2 +- crates/parser/src/parser/items/mod.rs | 4 +-- mist-test/src/items/class_test.mist | 2 +- 7 files changed, 23 insertions(+), 39 deletions(-) diff --git a/crates/codegen/src/class_decl.rs b/crates/codegen/src/class_decl.rs index 237c293..432142c 100644 --- a/crates/codegen/src/class_decl.rs +++ b/crates/codegen/src/class_decl.rs @@ -11,18 +11,12 @@ pub fn class_decl( visibility: &Visibility, name: &Identifier, generics: &GenericsDecl, - inherits: &Option, + inherits: &Option, fields: &Vec>, constructor: &Spanned, items: &Vec, ) { - let inherits = if let Some(inherits) = inherits { - let path = get_type_path(inherits); - ctx.expr_super = Some(path.clone()); - Some((inherits, path)) - } else { - None - }; + ctx.expr_super = inherits.clone(); // Struct decl cg.addln(&format!( @@ -37,9 +31,9 @@ pub fn class_decl( "pub _m_oop: (&'static [*const std::ffi::c_void; Self::__V_COUNT], *mut std::ffi::c_void),", ); - if let Some((inherits, _)) = inherits { + if let Some(inherits) = inherits { cg.add_indented("pub _super: Box<"); - cg.add(&inherits.get_rust()); + cg.add(&get_type_from_path(inherits).get_rust()); cg.addln(">,"); } @@ -112,7 +106,7 @@ pub fn class_decl( } // Super V Table - if let Some((inherits, inherit_path)) = &inherits { + if let Some(inherits) = &inherits { cg.add_indented(&format!( "pub const __SUPER_V_TABLE: [*const std::ffi::c_void; {}", inherits.get_rust() @@ -161,7 +155,7 @@ pub fn class_decl( match params.remove(0) { TypeExpr::Ref { mutable, .. } => { - cg.add_indented(&inherit_path.get_rust()); + cg.add_indented(&inherits.get_rust()); cg.add("::__m_"); cg.add(&i.item.name.get_rust()); cg.add(" as "); @@ -171,7 +165,7 @@ pub fn class_decl( TypeExpr::Ref { lifetime: None, mutable: mutable, - ty: Box::new((*inherits).clone()), + ty: Box::new(get_type_from_path(inherits)), }, ); @@ -229,7 +223,7 @@ pub fn class_decl( } } - if let Some((inherits, _)) = inherits { + if let Some(inherits) = inherits { cg.add("impl std::ops::Deref for "); cg.add(&name.get_rust()); @@ -451,12 +445,12 @@ pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustC cg.add_indentedln("}"); } -pub fn get_type_path(ty: &TypeExpr) -> Path { - match ty { - TypeExpr::Path(p, _) => p.clone(), - TypeExpr::Dyn(v) => get_type_path(v), - TypeExpr::Ref { ty, .. } => get_type_path(ty), - TypeExpr::UnsafePtr { ty, .. } => get_type_path(ty), - _ => unimplemented!(), - } +pub fn get_type_from_path(path: &ExprPath) -> TypeExpr { + let last = path.0.last().unwrap(); + + let mut path = path.0.iter().map(|v| v.ident.clone()).collect::>(); + + path.push(last.ident.clone()); + + TypeExpr::Path(Path(path), last.generics.clone()) } diff --git a/crates/codegen/src/expr.rs b/crates/codegen/src/expr.rs index e4e2701..f6fb129 100644 --- a/crates/codegen/src/expr.rs +++ b/crates/codegen/src/expr.rs @@ -20,17 +20,7 @@ impl GetRust for ExprPath { if self.0[0].ident.0 == "super" && self.0.len() == 1 { a.0[0].ident.0 = String::from("self._super"); } else if self.0[0].ident.0 == "Super" { - a.0[0].ident = path.0[0].clone(); - - if path.0.len() > 1 { - a.0.splice( - 1..1, - path.0.clone().into_iter().skip(1).map(|v| ExprPathSegment { - ident: v, - generics: None, - }), - ); - } + a.0[0] = path.0[0].clone(); } } diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index f6c466b..cbcc7ef 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -7,7 +7,7 @@ use mist_parser::ast::*; pub struct Context { pub expr_ensure_semicolon: bool, - pub expr_super: Option, + pub expr_super: Option, } pub trait GenRust { diff --git a/crates/parser/src/ast/top_level.rs b/crates/parser/src/ast/top_level.rs index b1196b6..5bb6ed1 100644 --- a/crates/parser/src/ast/top_level.rs +++ b/crates/parser/src/ast/top_level.rs @@ -54,7 +54,7 @@ pub enum TopLevelKind { visibility: Visibility, name: Identifier, generics: GenericsDecl, - inherits: Option, + inherits: Option, fields: Vec>, constructor: Spanned, items: Vec, diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 3a0d648..67ab7d8 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -294,7 +294,7 @@ class_item = _{ } class_decl = { - visibility? ~ "class" ~ identifier ~ generics_decl? ~ (":" ~ type_expr)? ~ "{" ~ class_fields ~ class_constructor ~ class_item* ~ "}" + visibility? ~ "class" ~ identifier ~ generics_decl? ~ (":" ~ expr_path)? ~ "{" ~ class_fields ~ class_constructor ~ class_item* ~ "}" } // ====================================================== diff --git a/crates/parser/src/parser/items/mod.rs b/crates/parser/src/parser/items/mod.rs index 7b49a1a..ebaf0a4 100644 --- a/crates/parser/src/parser/items/mod.rs +++ b/crates/parser/src/parser/items/mod.rs @@ -69,8 +69,8 @@ impl<'a> TryFrom> for TopLevelKind { .transpose() .map(|v| v.unwrap_or_default()), - inherits: consume_rule(&mut inner, Rule::type_expr) - .map(TypeExpr::try_from) + inherits: consume_rule(&mut inner, Rule::expr_path) + .map(ExprPath::try_from) .transpose(), fields: collect_recovered(inner.next().unwrap().into_inner()), diff --git a/mist-test/src/items/class_test.mist b/mist-test/src/items/class_test.mist index b95f19d..ad6c555 100644 --- a/mist-test/src/items/class_test.mist +++ b/mist-test/src/items/class_test.mist @@ -21,7 +21,7 @@ pub class Dog : Animal { } } - pub override fn speak(*self) { + pub override(Animal) fn speak(*self) { println!("Woof!"); } }