Skip to content

Commit 6446550

Browse files
committed
feat: implement lowering for trait impl blocks
- Add lower_impl_block() method to LoweringContext - Convert each method in impl block to a TypedFunction - Lower impl methods as regular functions in HIR - Convert TypedMethodParam to TypedParameter for lowering Impl block methods are now properly lowered to HIR and compiled. Methods from impl blocks can be called as regular functions. TODO: Implement proper name mangling for trait methods to avoid conflicts.
1 parent f8a883e commit 6446550

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

crates/compiler/src/lowering.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,10 @@ impl LoweringContext {
587587
self.lower_enum(enum_decl)?;
588588
}
589589

590+
TypedDeclaration::Impl(impl_block) => {
591+
self.lower_impl_block(impl_block)?;
592+
}
593+
590594
_ => {
591595
// NOTE: Other declaration types not yet lowered.
592596
// Includes: TypeAlias, Interface (trait lowering), etc.
@@ -1188,6 +1192,44 @@ impl LoweringContext {
11881192
Ok(())
11891193
}
11901194

1195+
/// Lower an impl block by extracting and lowering its methods
1196+
fn lower_impl_block(&mut self, impl_block: &zyntax_typed_ast::typed_ast::TypedTraitImpl) -> CompilerResult<()> {
1197+
use zyntax_typed_ast::TypedFunction;
1198+
1199+
// For each method in the impl block, convert it to a function and lower it
1200+
for method in &impl_block.methods {
1201+
// Create a function from the method
1202+
// Method names should be mangled to include the trait and type info
1203+
let func = TypedFunction {
1204+
name: method.name, // TODO: Consider name mangling for trait methods
1205+
type_params: vec![],
1206+
params: method.params.iter().map(|p| {
1207+
zyntax_typed_ast::TypedParameter {
1208+
name: p.name,
1209+
ty: p.ty.clone(),
1210+
mutability: p.mutability,
1211+
kind: p.kind.clone(),
1212+
default_value: p.default_value.clone(),
1213+
attributes: p.attributes.clone(),
1214+
span: p.span,
1215+
}
1216+
}).collect(),
1217+
return_type: method.return_type.clone(),
1218+
body: method.body.clone(),
1219+
visibility: zyntax_typed_ast::type_registry::Visibility::Public,
1220+
is_async: method.is_async,
1221+
is_external: false,
1222+
calling_convention: zyntax_typed_ast::type_registry::CallingConvention::Default,
1223+
link_name: None,
1224+
};
1225+
1226+
// Lower the method as a regular function
1227+
self.lower_function(&func)?;
1228+
}
1229+
1230+
Ok(())
1231+
}
1232+
11911233
/// Evaluate a TypedExpression as a compile-time constant
11921234
fn eval_const_expression(
11931235
&self,

0 commit comments

Comments
 (0)