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,
name: &Identifier,
generics: &GenericsDecl,
inherits: &Option<TypeExpr>,
inherits: &Option<ExprPath>,
fields: &Vec<Spanned<FieldDeclStmt>>,
constructor: &Spanned<ClassConstructor>,
items: &Vec<ClassItem>,
) {
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::<Vec<_>>();
path.push(last.ident.clone());
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 {
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();
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ use mist_parser::ast::*;
pub struct Context {
pub expr_ensure_semicolon: bool,
pub expr_super: Option<Path>,
pub expr_super: Option<ExprPath>,
}
pub trait GenRust {
+1 -1
View File
@@ -54,7 +54,7 @@ pub enum TopLevelKind {
visibility: Visibility,
name: Identifier,
generics: GenericsDecl,
inherits: Option<TypeExpr>,
inherits: Option<ExprPath>,
fields: Vec<Spanned<FieldDeclStmt>>,
constructor: Spanned<ClassConstructor>,
items: Vec<ClassItem>,
+1 -1
View File
@@ -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* ~ "}"
}
// ======================================================
+2 -2
View File
@@ -69,8 +69,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> 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()),