feat: add foreign function support

This commit is contained in:
2026-04-22 22:56:39 +02:00
parent 041a49e574
commit 8bcae8cb31
13 changed files with 368 additions and 46 deletions
+45 -1
View File
@@ -10,6 +10,7 @@ pub struct MirBuilder;
impl MirBuilder {
/// Builds a `MirModule` from a `TypedModule`.
pub fn build(module: &TypedModule) -> MirModule {
let mut extern_functions = Vec::new();
let mut functions = Vec::new();
for decl in &module.decls {
@@ -53,10 +54,25 @@ impl MirBuilder {
functions.push(builder.finish());
}
TypedDeclKind::ForeignFunction {
name,
params,
return_type,
..
} => {
extern_functions.push(MirExternFunction {
name: name.clone(),
params: params.iter().map(|(_, ty)| ty.clone()).collect(),
return_type: return_type.clone(),
});
}
}
}
MirModule { functions }
MirModule {
extern_functions,
functions,
}
}
}
@@ -399,6 +415,34 @@ impl FuncBuilder {
Operand::Copy(temp)
}
TypedExprKind::Call { callee, args } => {
let callee_name = match &callee.kind {
TypedExprKind::Identifier { name } => name.clone(),
_ => unimplemented!("indirect function calls are not yet supported"),
};
let mut arg_ops = Vec::new();
for arg in args {
arg_ops.push(self.lower_expr(arg));
}
let rval = Rvalue::Call(callee_name, arg_ops, expr.ty.clone());
if expr.ty == Ty::Unit {
self.emit_stmt(Statement {
kind: StatementKind::SideEffect(rval),
span: expr.span,
});
Operand::Constant(ConstantValue::Boolean(false)) // Dummy value for Unit assignments
} else {
let temp = self.new_temp(expr.ty.clone());
self.emit_stmt(Statement {
kind: StatementKind::Assign(temp, rval),
span: expr.span,
});
Operand::Copy(temp)
}
}
}
}
}