From b1ad4a1c280d25f92fdb1103edf2faa5e3e1daac Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Tue, 13 Aug 2024 17:28:34 -0500 Subject: Added type checking on function calls --- src/codegen.zig | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/codegen.zig') diff --git a/src/codegen.zig b/src/codegen.zig index 3922a75..cdcffdd 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -10,6 +10,7 @@ const types = llvm.types; const CodegenError = error{ Immutable, OutOfMemory, + IncorrectType, }; fn toLLVMtype(typ: parse.TypeIdent, sym: *symb.SymbolTable, expr: ?parse.NodeExpr) types.LLVMTypeRef { @@ -232,11 +233,16 @@ pub const Generator = struct { }, .call => |call| blk: { + std.debug.print("Function {s} requested\n", .{call.ident.ident}); + + const functype = expr.symtable.getValue(call.ident.ident).?.typ.Function; const ident = try self.allocator.dupeZ(u8, call.ident.ident); const function = core.LLVMGetNamedFunction(self.module, ident); var args = std.ArrayList(types.LLVMValueRef).init(self.allocator); - for (call.args) |arg| + for (call.args, functype.input) |arg, intype| { + if (!std.meta.eql(expr.symtable.getType(arg.typ.?).?, intype)) return CodegenError.IncorrectType; try args.append(try self.genExpr(arg)); + } const funcType = core.LLVMGlobalGetValueType(function); // std.debug.print("FUNCTYPE: {s}\n", .{call.ident.ident}); -- cgit v1.2.3