Updated codegen for class decl
This commit is contained in:
@@ -51,7 +51,7 @@ impl ClassProcessedData {
|
|||||||
if matches!(method.item.visibility, Visibility::Public) {
|
if matches!(method.item.visibility, Visibility::Public) {
|
||||||
match &method.item.is_override {
|
match &method.item.is_override {
|
||||||
None => {
|
None => {
|
||||||
if method.item.is_using_self() {
|
if method.item.self_param.is_some() {
|
||||||
v_table.push(method.item.name.clone());
|
v_table.push(method.item.name.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -263,7 +263,7 @@ impl ClassProcessedData {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let TypeExpr::Ref { mutable, .. } = params.remove(0) {
|
if let Some((_, _, mutable)) = &method.item.self_param {
|
||||||
method.gen_span(cg);
|
method.gen_span(cg);
|
||||||
cg.add_indented(&format!("{}::__m_", target_rust_path));
|
cg.add_indented(&format!("{}::__m_", target_rust_path));
|
||||||
cg.add(&method.item.name.get_rust());
|
cg.add(&method.item.name.get_rust());
|
||||||
@@ -273,7 +273,7 @@ impl ClassProcessedData {
|
|||||||
0,
|
0,
|
||||||
TypeExpr::Ref {
|
TypeExpr::Ref {
|
||||||
lifetime: None,
|
lifetime: None,
|
||||||
mutable,
|
mutable: *mutable,
|
||||||
ty: Box::new(get_type_from_path(&target_path)),
|
ty: Box::new(get_type_from_path(&target_path)),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -396,7 +396,7 @@ impl ClassProcessedData {
|
|||||||
for method in &self.methods {
|
for method in &self.methods {
|
||||||
match method.item.visibility {
|
match method.item.visibility {
|
||||||
Visibility::Public => {
|
Visibility::Public => {
|
||||||
if method.item.is_using_self() {
|
if method.item.self_param.is_some() {
|
||||||
if method.item.is_override.is_none() {
|
if method.item.is_override.is_none() {
|
||||||
gen_method_point(&method.item, ctx, cg);
|
gen_method_point(&method.item, ctx, cg);
|
||||||
}
|
}
|
||||||
@@ -500,6 +500,7 @@ fn construct_pattern(pat: &Pattern, idx: usize) -> Pattern {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustCodegen) {
|
pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustCodegen) {
|
||||||
|
let mutable_self = if let Some((is_ref, lifetime, is_mut)) = &method.self_param {
|
||||||
cg.add_indented(&format!(
|
cg.add_indented(&format!(
|
||||||
"{}fn {}{}(",
|
"{}fn {}{}(",
|
||||||
method.visibility.get_rust(),
|
method.visibility.get_rust(),
|
||||||
@@ -507,6 +508,25 @@ pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustC
|
|||||||
method.generics.get_rust(),
|
method.generics.get_rust(),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
if *is_ref {
|
||||||
|
cg.add("&");
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(lifetime) = lifetime {
|
||||||
|
cg.add(&format!("'{} ", lifetime.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
if *is_mut {
|
||||||
|
cg.add("mut ");
|
||||||
|
}
|
||||||
|
|
||||||
|
cg.add("self,");
|
||||||
|
|
||||||
|
*is_mut
|
||||||
|
} else {
|
||||||
|
panic!();
|
||||||
|
};
|
||||||
|
|
||||||
let params = method
|
let params = method
|
||||||
.params
|
.params
|
||||||
.0
|
.0
|
||||||
@@ -553,14 +573,10 @@ pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustC
|
|||||||
.filter_map(|v| v.type_)
|
.filter_map(|v| v.type_)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let TypeExpr::Ref { mutable, .. } = param_types.remove(0) else {
|
|
||||||
panic!("")
|
|
||||||
};
|
|
||||||
|
|
||||||
param_types.insert(
|
param_types.insert(
|
||||||
0,
|
0,
|
||||||
TypeExpr::UnsafePtr {
|
TypeExpr::UnsafePtr {
|
||||||
mutable,
|
mutable: mutable_self,
|
||||||
ty: Box::new(TypeExpr::Path(
|
ty: Box::new(TypeExpr::Path(
|
||||||
Path(vec![
|
Path(vec![
|
||||||
Identifier(String::from("std")),
|
Identifier(String::from("std")),
|
||||||
@@ -575,16 +591,13 @@ pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustC
|
|||||||
cg.add(&TypeExpr::StaticFn(param_types, method.return_type.clone().map(Box::new)).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.addln(" = std::mem::transmute(func_ptr);");
|
||||||
|
|
||||||
if mutable {
|
if mutable_self {
|
||||||
cg.add_indented("func(self as *mut Self as *const std::ffi::c_void");
|
cg.add_indented("func(self as *mut Self as *const std::ffi::c_void");
|
||||||
} else {
|
} else {
|
||||||
cg.add_indented("func(self as *const Self as *const std::ffi::c_void");
|
cg.add_indented("func(self as *const Self as *const std::ffi::c_void");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i, param) in ¶ms {
|
for (_, param) in ¶ms {
|
||||||
if *i == 0 {
|
|
||||||
continue; // self already fulfills it
|
|
||||||
}
|
|
||||||
cg.add(", ");
|
cg.add(", ");
|
||||||
ctx.expr_ensure_semicolon = false;
|
ctx.expr_ensure_semicolon = false;
|
||||||
param.name.gen_rust(ctx, cg);
|
param.name.gen_rust(ctx, cg);
|
||||||
|
|||||||
@@ -126,18 +126,3 @@ pub struct FieldDeclStmt {
|
|||||||
pub decl: FieldDecl,
|
pub decl: FieldDecl,
|
||||||
pub init: Option<Expression>,
|
pub init: Option<Expression>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FunctionDecl {
|
|
||||||
pub fn is_using_self(&self) -> bool {
|
|
||||||
match self.params.0.get(0) {
|
|
||||||
Some(VarDecl { name, .. }) => {
|
|
||||||
if let Pattern::Path(_, v) = name {
|
|
||||||
v.0.len() == 1 && v.0[0].0 == "self"
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ pub fn find_function_call<'a>(
|
|||||||
) -> Option<&'a Spanned<FunctionDecl>> {
|
) -> Option<&'a Spanned<FunctionDecl>> {
|
||||||
items.iter().find_map(|v| {
|
items.iter().find_map(|v| {
|
||||||
if let ClassItem::Method(m) = v {
|
if let ClassItem::Method(m) = v {
|
||||||
if m.item.is_using_self() && &m.item.name == name {
|
if m.item.self_param.is_some() && &m.item.name == name {
|
||||||
Some(m)
|
Some(m)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ pub class Animal {
|
|||||||
"Unknown".to_string()
|
"Unknown".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub void test_speak(&self) {
|
pub void test_speak<'a>(&'a self) {
|
||||||
assert_eq!(self.speak(), "Woof!");
|
assert_eq!(self.speak(), "Woof!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user