generics codege

This commit is contained in:
2026-05-08 16:54:11 +02:00
parent 205e2d9fd3
commit 0e6c73e85c
2 changed files with 38 additions and 4 deletions
+36 -4
View File
@@ -1,6 +1,6 @@
use parser::ast::{
Attribute, BinaryOp, Block, EnumItem, Expression, FunctionDecl, Identifier, Literal, Path,
Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr,
Attribute, BinaryOp, Block, EnumItem, Expression, FunctionDecl, Generics, Identifier, Literal,
Path, Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr,
TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, Visibility,
};
@@ -316,12 +316,14 @@ impl ToRust for TopLevelKind {
Self::StructDecl {
visibility,
name,
generics,
fields,
} => {
cg.addln(&format!(
"{}struct {} {{",
"{}struct {}{} {{",
visibility.get_rust(),
name.get_rust()
name.get_rust(),
generics.get_rust()
));
cg.indent += 1;
@@ -703,6 +705,36 @@ impl GetRust for Pattern {
}
}
impl GetRust for Generics {
fn get_rust(&self) -> String {
if self.0.len() == 0 {
String::new()
} else {
format!(
"<{}>",
self.0
.iter()
.map(|generic| generic.0.get_rust()
+ &(if generic.1.len() == 0 {
String::new()
} else {
format!(
": {}",
generic
.1
.iter()
.map(Path::get_rust)
.collect::<Vec<_>>()
.join("+")
)
}))
.collect::<Vec<_>>()
.join(", ")
)
}
}
}
pub fn get_static_type_path(path: &Path) -> String {
let rust_path = path.get_rust();