Add metadata location to function calls

This commit is contained in:
Sofia 2025-07-20 21:08:15 +03:00
parent 33d5ee03f0
commit d7661cb968
6 changed files with 59 additions and 22 deletions

View File

@ -2,8 +2,8 @@
extern fn puts(message: string) -> i32; extern fn puts(message: string) -> i32;
struct DivT { struct DivT {
quot: i32, quotient: i32,
rem: i32, remainder: i32,
} }
extern fn div(numerator: i32, denominator: i32) -> DivT; extern fn div(numerator: i32, denominator: i32) -> DivT;

View File

@ -89,11 +89,7 @@ impl BinaryOperator {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FunctionCallExpression( pub struct FunctionCallExpression(pub String, pub Vec<Expression>, pub TokenRange);
pub String,
pub Vec<Expression>,
#[allow(dead_code)] pub TokenRange,
);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct IfExpression( pub struct IfExpression(

View File

@ -188,6 +188,7 @@ impl ast::Expression {
.iter() .iter()
.map(|e| e.process(module_id)) .map(|e| e.process(module_id))
.collect(), .collect(),
meta: fn_call_expr.2.as_meta(module_id),
}) })
} }
ast::ExpressionKind::BlockExpr(block) => { ast::ExpressionKind::BlockExpr(block) => {

View File

@ -648,11 +648,13 @@ impl mir::Expression {
)) ))
} }
mir::ExprKind::FunctionCall(call) => { mir::ExprKind::FunctionCall(call) => {
let ret_type = call let ret_type_kind = call
.return_type .return_type
.known() .known()
.expect("function return type unknown"); .expect("function return type unknown");
let ret_type = ret_type_kind.get_type(scope.type_values, scope.types);
let params = call let params = call
.parameters .parameters
.iter() .iter()
@ -663,18 +665,56 @@ impl mir::Expression {
.get(&call.name) .get(&call.name)
.expect("function not found!"); .expect("function not found!");
dbg!(&self, &callee.ir.value()); dbg!(&self, &callee.ir.value());
Some(StackValue(
StackValueKind::Immutable( let val = scope
scope .block
.block .build(
.build( call.name.clone(),
call.name.clone(), Instr::FunctionCall(callee.ir.value(), params),
Instr::FunctionCall(callee.ir.value(), params), )
) .unwrap();
.unwrap(),
), if let Some(debug) = &scope.debug {
ret_type, let location = call.meta.into_debug(scope.tokens).unwrap();
)) let location_val = debug.info.location(&debug.scope, location);
val.with_location(&mut scope.block, location_val);
}
let ptr = if ret_type_kind != TypeKind::Void {
let ptr = scope
.block
.build(&call.name, Instr::Alloca(ret_type.clone()))
.unwrap();
scope
.block
.build(format!("{}.store", call.name), Instr::Store(ptr, val))
.unwrap();
Some(ptr)
} else {
None
};
if let Some(ptr) = ptr {
if state.should_load {
Some(StackValue(
StackValueKind::Immutable(
scope
.block
.build(call.name.clone(), Instr::Load(ptr, ret_type))
.unwrap(),
),
ret_type_kind,
))
} else {
Some(StackValue(
StackValueKind::Immutable(ptr),
TypeKind::Ptr(Box::new(ret_type_kind)),
))
}
} else {
None
}
} }
mir::ExprKind::If(if_expression) => if_expression.codegen(scope, state), mir::ExprKind::If(if_expression) => if_expression.codegen(scope, state),
mir::ExprKind::Block(block) => { mir::ExprKind::Block(block) => {

View File

@ -259,6 +259,7 @@ pub struct FunctionCall {
pub name: String, pub name: String,
pub return_type: TypeKind, pub return_type: TypeKind,
pub parameters: Vec<Expression>, pub parameters: Vec<Expression>,
pub meta: Metadata,
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -5,8 +5,7 @@ import std::intdiv;
fn main() -> i32 { fn main() -> i32 {
let hello = "hello world"; let hello = "hello world";
print(hello); print(hello);
return intdiv(10, 5).quot; return intdiv(15, 5).quotient;
} }