Improved override

This commit is contained in:
2026-06-09 13:51:32 +02:00
parent 0347892056
commit a9f6cce1b3
7 changed files with 23 additions and 39 deletions
+16 -22
View File
@@ -11,18 +11,12 @@ pub fn class_decl(
visibility: &Visibility, visibility: &Visibility,
name: &Identifier, name: &Identifier,
generics: &GenericsDecl, generics: &GenericsDecl,
inherits: &Option<TypeExpr>, inherits: &Option<ExprPath>,
fields: &Vec<Spanned<FieldDeclStmt>>, fields: &Vec<Spanned<FieldDeclStmt>>,
constructor: &Spanned<ClassConstructor>, constructor: &Spanned<ClassConstructor>,
items: &Vec<ClassItem>, items: &Vec<ClassItem>,
) { ) {
let inherits = if let Some(inherits) = inherits { ctx.expr_super = inherits.clone();
let path = get_type_path(inherits);
ctx.expr_super = Some(path.clone());
Some((inherits, path))
} else {
None
};
// Struct decl // Struct decl
cg.addln(&format!( 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),", "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_indented("pub _super: Box<");
cg.add(&inherits.get_rust()); cg.add(&get_type_from_path(inherits).get_rust());
cg.addln(">,"); cg.addln(">,");
} }
@@ -112,7 +106,7 @@ pub fn class_decl(
} }
// Super V Table // Super V Table
if let Some((inherits, inherit_path)) = &inherits { if let Some(inherits) = &inherits {
cg.add_indented(&format!( cg.add_indented(&format!(
"pub const __SUPER_V_TABLE: [*const std::ffi::c_void; {}", "pub const __SUPER_V_TABLE: [*const std::ffi::c_void; {}",
inherits.get_rust() inherits.get_rust()
@@ -161,7 +155,7 @@ pub fn class_decl(
match params.remove(0) { match params.remove(0) {
TypeExpr::Ref { mutable, .. } => { TypeExpr::Ref { mutable, .. } => {
cg.add_indented(&inherit_path.get_rust()); cg.add_indented(&inherits.get_rust());
cg.add("::__m_"); cg.add("::__m_");
cg.add(&i.item.name.get_rust()); cg.add(&i.item.name.get_rust());
cg.add(" as "); cg.add(" as ");
@@ -171,7 +165,7 @@ pub fn class_decl(
TypeExpr::Ref { TypeExpr::Ref {
lifetime: None, lifetime: None,
mutable: mutable, 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("impl std::ops::Deref for ");
cg.add(&name.get_rust()); 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("}"); cg.add_indentedln("}");
} }
pub fn get_type_path(ty: &TypeExpr) -> Path { pub fn get_type_from_path(path: &ExprPath) -> TypeExpr {
match ty { let last = path.0.last().unwrap();
TypeExpr::Path(p, _) => p.clone(),
TypeExpr::Dyn(v) => get_type_path(v), let mut path = path.0.iter().map(|v| v.ident.clone()).collect::<Vec<_>>();
TypeExpr::Ref { ty, .. } => get_type_path(ty),
TypeExpr::UnsafePtr { ty, .. } => get_type_path(ty), path.push(last.ident.clone());
_ => unimplemented!(),
} TypeExpr::Path(Path(path), last.generics.clone())
} }
+1 -11
View File
@@ -20,17 +20,7 @@ impl GetRust for ExprPath {
if self.0[0].ident.0 == "super" && self.0.len() == 1 { if self.0[0].ident.0 == "super" && self.0.len() == 1 {
a.0[0].ident.0 = String::from("self._super"); a.0[0].ident.0 = String::from("self._super");
} else if self.0[0].ident.0 == "Super" { } else if self.0[0].ident.0 == "Super" {
a.0[0].ident = path.0[0].clone(); a.0[0] = 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,
}),
);
}
} }
} }
+1 -1
View File
@@ -7,7 +7,7 @@ use mist_parser::ast::*;
pub struct Context { pub struct Context {
pub expr_ensure_semicolon: bool, pub expr_ensure_semicolon: bool,
pub expr_super: Option<Path>, pub expr_super: Option<ExprPath>,
} }
pub trait GenRust { pub trait GenRust {
+1 -1
View File
@@ -54,7 +54,7 @@ pub enum TopLevelKind {
visibility: Visibility, visibility: Visibility,
name: Identifier, name: Identifier,
generics: GenericsDecl, generics: GenericsDecl,
inherits: Option<TypeExpr>, inherits: Option<ExprPath>,
fields: Vec<Spanned<FieldDeclStmt>>, fields: Vec<Spanned<FieldDeclStmt>>,
constructor: Spanned<ClassConstructor>, constructor: Spanned<ClassConstructor>,
items: Vec<ClassItem>, items: Vec<ClassItem>,
+1 -1
View File
@@ -294,7 +294,7 @@ class_item = _{
} }
class_decl = { 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* ~ "}"
} }
// ====================================================== // ======================================================
+2 -2
View File
@@ -69,8 +69,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.transpose() .transpose()
.map(|v| v.unwrap_or_default()), .map(|v| v.unwrap_or_default()),
inherits: consume_rule(&mut inner, Rule::type_expr) inherits: consume_rule(&mut inner, Rule::expr_path)
.map(TypeExpr::try_from) .map(ExprPath::try_from)
.transpose(), .transpose(),
fields: collect_recovered(inner.next().unwrap().into_inner()), fields: collect_recovered(inner.next().unwrap().into_inner()),
+1 -1
View File
@@ -21,7 +21,7 @@ pub class Dog : Animal {
} }
} }
pub override fn speak(*self) { pub override(Animal<i32>) fn speak(*self) {
println!("Woof!"); println!("Woof!");
} }
} }