more forward declarations
This commit is contained in:
176
src/codegen.zig
176
src/codegen.zig
@@ -201,6 +201,8 @@ pub const CodeGen = struct {
|
||||
current_match_tags: ?[]const u64 = null,
|
||||
// Functions deferred to compile after all types are registered (e.g. any_to_string)
|
||||
deferred_fn_bodies: std.ArrayList(DeferredFn),
|
||||
// AST nodes whose bodies were generated in Pass 4 (to avoid double generation in main)
|
||||
generated_bodies: std.AutoHashMap(*const Node, void),
|
||||
// Libraries to link against (from #library directives)
|
||||
foreign_libraries: std.ArrayList([]const u8),
|
||||
// Set of foreign function names (for ABI lowering at call sites)
|
||||
@@ -436,6 +438,7 @@ pub const CodeGen = struct {
|
||||
.any_type_id_map = std.StringHashMap(u64).init(allocator),
|
||||
.any_type_entries = std.StringHashMap(AnyTypeEntry).init(allocator),
|
||||
.deferred_fn_bodies = std.ArrayList(DeferredFn).empty,
|
||||
.generated_bodies = std.AutoHashMap(*const Node, void).init(allocator),
|
||||
.foreign_libraries = std.ArrayList([]const u8).empty,
|
||||
.foreign_fns = std.StringHashMap(void).init(allocator),
|
||||
.library_constants = std.StringHashMap([]const u8).init(allocator),
|
||||
@@ -1392,7 +1395,7 @@ pub const CodeGen = struct {
|
||||
switch (decl.data) {
|
||||
.fn_decl => |fd| {
|
||||
if (fd.body.data != .builtin_expr and fd.type_params.len == 0) {
|
||||
try self.registerFnDecl(fd, fd.name);
|
||||
_ = try self.registerFnDecl(fd, fd.name);
|
||||
}
|
||||
},
|
||||
.struct_decl => |sd| try self.registerStructMethods(sd),
|
||||
@@ -1457,11 +1460,13 @@ pub const CodeGen = struct {
|
||||
} else {
|
||||
try self.genFnBody(fd, fd.name);
|
||||
}
|
||||
try self.generated_bodies.put(decl, {});
|
||||
}
|
||||
},
|
||||
.const_decl => |cd| {
|
||||
if (cd.value.data == .lambda) {
|
||||
try self.genLambdaBody(cd.name, cd.value.data.lambda);
|
||||
try self.generated_bodies.put(decl, {});
|
||||
}
|
||||
},
|
||||
.namespace_decl => |ns| {
|
||||
@@ -2338,7 +2343,7 @@ pub const CodeGen = struct {
|
||||
}
|
||||
}
|
||||
|
||||
fn registerFnDecl(self: *CodeGen, fd: ast.FnDecl, llvm_name: []const u8) !void {
|
||||
fn registerFnDecl(self: *CodeGen, fd: ast.FnDecl, llvm_name: []const u8) !c.LLVMValueRef {
|
||||
const is_foreign = fd.body.data == .foreign_expr;
|
||||
// For foreign functions: resolve C symbol name (rename) and validate library ref
|
||||
const actual_llvm_name = if (is_foreign) blk: {
|
||||
@@ -2359,7 +2364,7 @@ pub const CodeGen = struct {
|
||||
} else llvm_name;
|
||||
const fn_type = try self.buildFnType(fd.params, fd.return_type, fd.name, is_foreign);
|
||||
const name_z = try self.allocator.dupeZ(u8, actual_llvm_name);
|
||||
_ = c.LLVMAddFunction(self.module, name_z.ptr, fn_type);
|
||||
const function = c.LLVMAddFunction(self.module, name_z.ptr, fn_type);
|
||||
// Track foreign functions for ABI lowering at call sites (use sx name for call-site lookup)
|
||||
if (is_foreign) {
|
||||
try self.foreign_fns.put(llvm_name, {});
|
||||
@@ -2394,6 +2399,7 @@ pub const CodeGen = struct {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return function;
|
||||
}
|
||||
|
||||
/// registerTypes helper: register type names within a namespace.
|
||||
@@ -2473,7 +2479,7 @@ pub const CodeGen = struct {
|
||||
}
|
||||
const qualified = try std.fmt.allocPrint(self.allocator, "{s}.{s}", .{ ns.name, fd.name });
|
||||
if (fd.body.data == .foreign_expr) {
|
||||
try self.registerFnDecl(fd, fd.name);
|
||||
_ = try self.registerFnDecl(fd, fd.name);
|
||||
try self.foreign_fns.put(qualified, {});
|
||||
const fe = fd.body.data.foreign_expr;
|
||||
if (fe.c_name) |c_name| {
|
||||
@@ -2490,7 +2496,7 @@ pub const CodeGen = struct {
|
||||
} else if (fd.type_params.len > 0) {
|
||||
try self.generic_templates.put(qualified, fd);
|
||||
} else {
|
||||
try self.registerFnDecl(fd, qualified);
|
||||
_ = try self.registerFnDecl(fd, qualified);
|
||||
}
|
||||
},
|
||||
.struct_decl => |sd| {
|
||||
@@ -2980,8 +2986,10 @@ pub const CodeGen = struct {
|
||||
if (ret_val) |val| {
|
||||
const prepared = try self.prepareReturnValue(val, ret_sx_type);
|
||||
self.ret(prepared);
|
||||
} else {
|
||||
} else if (ret_sx_type == .void_type) {
|
||||
self.retVoid();
|
||||
} else {
|
||||
_ = c.LLVMBuildUnreachable(self.builder);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3014,30 +3022,45 @@ pub const CodeGen = struct {
|
||||
// Infer return type from body for => lambdas without explicit annotation
|
||||
const ret_sx_type = if (fd.return_type != null) self.resolveType(fd.return_type) else if (fd.is_arrow) self.inferType(fd.body) else Type.void_type;
|
||||
|
||||
// For arrow lambdas with inferred return type, build function manually
|
||||
if (fd.is_arrow and fd.return_type == null) {
|
||||
const ret_llvm_type = self.typeToLLVM(ret_sx_type);
|
||||
var param_llvm_types = std.ArrayList(c.LLVMTypeRef).empty;
|
||||
for (fd.params) |param| {
|
||||
try param_llvm_types.append(self.allocator, self.typeToLLVM(self.resolveType(param.type_expr)));
|
||||
// Build or register the LLVM function, keeping a direct reference
|
||||
// (LLVMGetNamedFunction returns the first fn with that name, which
|
||||
// may differ when multiple local functions share a name)
|
||||
const function = blk: {
|
||||
if (fd.is_arrow and fd.return_type == null) {
|
||||
const ret_llvm_type = self.typeToLLVM(ret_sx_type);
|
||||
var param_llvm_types = std.ArrayList(c.LLVMTypeRef).empty;
|
||||
for (fd.params) |param| {
|
||||
try param_llvm_types.append(self.allocator, self.typeToLLVM(self.resolveType(param.type_expr)));
|
||||
}
|
||||
const params_slice = try param_llvm_types.toOwnedSlice(self.allocator);
|
||||
const fn_type = c.LLVMFunctionType(
|
||||
ret_llvm_type,
|
||||
if (params_slice.len > 0) params_slice.ptr else null,
|
||||
@intCast(params_slice.len),
|
||||
0,
|
||||
);
|
||||
const name_z2 = try self.allocator.dupeZ(u8, fd.name);
|
||||
const func = c.LLVMAddFunction(self.module, name_z2.ptr, fn_type);
|
||||
try self.function_return_types.put(fd.name, ret_sx_type);
|
||||
break :blk func;
|
||||
} else {
|
||||
break :blk try self.registerFnDecl(fd, fd.name);
|
||||
}
|
||||
const params_slice = try param_llvm_types.toOwnedSlice(self.allocator);
|
||||
const fn_type = c.LLVMFunctionType(
|
||||
ret_llvm_type,
|
||||
if (params_slice.len > 0) params_slice.ptr else null,
|
||||
@intCast(params_slice.len),
|
||||
0,
|
||||
);
|
||||
const name_z2 = try self.allocator.dupeZ(u8, fd.name);
|
||||
_ = c.LLVMAddFunction(self.module, name_z2.ptr, fn_type);
|
||||
try self.function_return_types.put(fd.name, ret_sx_type);
|
||||
} else {
|
||||
try self.registerFnDecl(fd, fd.name);
|
||||
};
|
||||
|
||||
// Skip if this exact AST node was already generated in Pass 4
|
||||
// (top-level fn_decls appear both in Pass 4 and main's body)
|
||||
if (self.generated_bodies.contains(node)) {
|
||||
self.named_values.deinit();
|
||||
self.named_values = saved_named;
|
||||
self.narrowed_types = saved_narrowed;
|
||||
self.current_return_type = saved_ret;
|
||||
self.current_function = saved_fn;
|
||||
self.positionAt(saved_bb);
|
||||
return null;
|
||||
}
|
||||
|
||||
self.current_return_type = ret_sx_type;
|
||||
const name_z = try self.allocator.dupeZ(u8, fd.name);
|
||||
const function = c.LLVMGetNamedFunction(self.module, name_z.ptr) orelse
|
||||
return self.emitErrorFmt("local function '{s}' not found", .{fd.name});
|
||||
self.current_function = function;
|
||||
_ = self.appendBlock(function, "entry");
|
||||
|
||||
@@ -3071,7 +3094,7 @@ pub const CodeGen = struct {
|
||||
const ret_val = try self.prepareReturnValue(val, ret_sx_type);
|
||||
self.ret(ret_val);
|
||||
} else {
|
||||
self.retVoid();
|
||||
_ = c.LLVMBuildUnreachable(self.builder);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3081,6 +3104,25 @@ pub const CodeGen = struct {
|
||||
self.current_return_type = saved_ret;
|
||||
self.current_function = saved_fn;
|
||||
self.positionAt(saved_bb);
|
||||
|
||||
// Register local function in outer scope's named_values so it
|
||||
// shadows any top-level function with the same name.
|
||||
{
|
||||
var param_types_list = std.ArrayList(Type).empty;
|
||||
for (fd.params) |param| {
|
||||
try param_types_list.append(self.allocator, self.resolveType(param.type_expr));
|
||||
}
|
||||
const ret_type_ptr = try self.allocator.create(Type);
|
||||
ret_type_ptr.* = ret_sx_type;
|
||||
const fn_ty: Type = .{ .function_type = .{
|
||||
.param_types = try param_types_list.toOwnedSlice(self.allocator),
|
||||
.return_type = ret_type_ptr,
|
||||
} };
|
||||
const local_name_z = try self.allocator.dupeZ(u8, fd.name);
|
||||
const fn_alloca = self.buildEntryBlockAlloca(self.ptrType(), local_name_z.ptr);
|
||||
_ = c.LLVMBuildStore(self.builder, function, fn_alloca);
|
||||
try self.named_values.put(fd.name, .{ .ptr = fn_alloca, .ty = fn_ty });
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
@@ -4841,7 +4883,7 @@ pub const CodeGen = struct {
|
||||
try self.generic_templates.put(qualified, fd);
|
||||
} else {
|
||||
// Non-generic struct, non-generic method: register directly
|
||||
try self.registerFnDecl(fd, qualified);
|
||||
_ = try self.registerFnDecl(fd, qualified);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4851,6 +4893,9 @@ pub const CodeGen = struct {
|
||||
fn registerProtocolDecl(self: *CodeGen, pd: ast.ProtocolDecl) !void {
|
||||
try self.protocol_decls.put(pd.name, pd);
|
||||
|
||||
// Skip if already registered (can happen with diamond imports)
|
||||
if (self.type_registry.contains(pd.name)) return;
|
||||
|
||||
if (pd.is_inline) {
|
||||
// #inline protocol: generate struct { ctx: *void, method1: fn_ptr, method2: fn_ptr, ... }
|
||||
const n_fields = 1 + pd.methods.len; // ctx + one fn-ptr per method
|
||||
@@ -5188,7 +5233,7 @@ pub const CodeGen = struct {
|
||||
try self.generic_templates.put(qualified, fd);
|
||||
} else {
|
||||
// Non-generic: register directly
|
||||
try self.registerFnDecl(fd, qualified);
|
||||
_ = try self.registerFnDecl(fd, qualified);
|
||||
}
|
||||
try self.fn_signatures.put(qualified, self.buildFnSignature(fd));
|
||||
}
|
||||
@@ -5211,7 +5256,7 @@ pub const CodeGen = struct {
|
||||
// Synthesize a fn_decl: method_name :: (self: *ConcreteType, params...) -> R { default_body }
|
||||
const qualified = try std.fmt.allocPrint(self.allocator, "{s}.{s}", .{ ib.target_type, method.name });
|
||||
const self_fd = try self.synthesizeDefaultMethod(ib.target_type, method);
|
||||
try self.registerFnDecl(self_fd, qualified);
|
||||
_ = try self.registerFnDecl(self_fd, qualified);
|
||||
try self.fn_signatures.put(qualified, self.buildFnSignature(self_fd));
|
||||
}
|
||||
}
|
||||
@@ -7931,30 +7976,6 @@ pub const CodeGen = struct {
|
||||
return self.genCallByName(resolved, call_node);
|
||||
}
|
||||
|
||||
// Check if this is a generic function call
|
||||
if (self.generic_templates.get(callee_name)) |template| {
|
||||
return self.genGenericCall(callee_name, template, call_node);
|
||||
}
|
||||
// Intra-namespace fallback for generic templates
|
||||
if (self.current_namespace) |ns| {
|
||||
const qualified = try std.fmt.allocPrint(self.allocator, "{s}.{s}", .{ ns, callee_name });
|
||||
if (self.generic_templates.get(qualified)) |template| {
|
||||
return self.genGenericCall(qualified, template, call_node);
|
||||
}
|
||||
}
|
||||
|
||||
// Check for #builtin function (only available when imported)
|
||||
if (self.builtin_functions.contains(callee_name)) {
|
||||
return self.dispatchBuiltin(callee_name, call_node);
|
||||
}
|
||||
// Intra-namespace fallback for builtins
|
||||
if (self.current_namespace) |ns| {
|
||||
const qualified_builtin = try std.fmt.allocPrint(self.allocator, "{s}.{s}", .{ ns, callee_name });
|
||||
if (self.builtin_functions.contains(qualified_builtin)) {
|
||||
return self.dispatchBuiltin(qualified_builtin, call_node);
|
||||
}
|
||||
}
|
||||
|
||||
// Compiler intrinsics (always available, no #builtin declaration needed)
|
||||
if (std.mem.eql(u8, callee_name, "sqrt")) {
|
||||
return self.genMathIntrinsic(call_node, "sqrt");
|
||||
@@ -7981,7 +8002,8 @@ pub const CodeGen = struct {
|
||||
return self.genClosureIntrinsic(call_node);
|
||||
}
|
||||
|
||||
// Local variable takes priority: closures and function pointers shadow LLVM named functions
|
||||
// Local variables shadow imported functions: closures and function pointers
|
||||
// take priority over generic templates, builtins, and LLVM named functions.
|
||||
if (self.lookupValue(callee_name)) |v| {
|
||||
const entry = v.asNamedValue();
|
||||
if (entry) |e| {
|
||||
@@ -7994,6 +8016,30 @@ pub const CodeGen = struct {
|
||||
}
|
||||
}
|
||||
|
||||
// Check if this is a generic function call
|
||||
if (self.generic_templates.get(callee_name)) |template| {
|
||||
return self.genGenericCall(callee_name, template, call_node);
|
||||
}
|
||||
// Intra-namespace fallback for generic templates
|
||||
if (self.current_namespace) |ns| {
|
||||
const qualified = try std.fmt.allocPrint(self.allocator, "{s}.{s}", .{ ns, callee_name });
|
||||
if (self.generic_templates.get(qualified)) |template| {
|
||||
return self.genGenericCall(qualified, template, call_node);
|
||||
}
|
||||
}
|
||||
|
||||
// Check for #builtin function (only available when imported)
|
||||
if (self.builtin_functions.contains(callee_name)) {
|
||||
return self.dispatchBuiltin(callee_name, call_node);
|
||||
}
|
||||
// Intra-namespace fallback for builtins
|
||||
if (self.current_namespace) |ns| {
|
||||
const qualified_builtin = try std.fmt.allocPrint(self.allocator, "{s}.{s}", .{ ns, callee_name });
|
||||
if (self.builtin_functions.contains(qualified_builtin)) {
|
||||
return self.dispatchBuiltin(qualified_builtin, call_node);
|
||||
}
|
||||
}
|
||||
|
||||
var nbuf: [256]u8 = undefined;
|
||||
var callee_fn = c.LLVMGetNamedFunction(self.module, self.nameToCStr(callee_name, &nbuf));
|
||||
// Foreign function fallback: qualified name "ns.Func" → try unqualified "Func" (the C symbol)
|
||||
@@ -8417,7 +8463,23 @@ pub const CodeGen = struct {
|
||||
if (ret_llvm == self.voidType()) {
|
||||
_ = c.LLVMBuildRetVoid(self.builder);
|
||||
} else {
|
||||
_ = c.LLVMBuildRet(self.builder, result);
|
||||
// Convert result type if it doesn't match the thunk's declared return type
|
||||
var ret_val = result;
|
||||
const result_ty = c.LLVMTypeOf(result);
|
||||
if (result_ty != ret_llvm) {
|
||||
const src_kind = c.LLVMGetTypeKind(result_ty);
|
||||
const dst_kind = c.LLVMGetTypeKind(ret_llvm);
|
||||
if (src_kind == c.LLVMIntegerTypeKind and dst_kind == c.LLVMIntegerTypeKind) {
|
||||
const src_bits = c.LLVMGetIntTypeWidth(result_ty);
|
||||
const dst_bits = c.LLVMGetIntTypeWidth(ret_llvm);
|
||||
if (src_bits > dst_bits) {
|
||||
ret_val = c.LLVMBuildTrunc(self.builder, result, ret_llvm, "thunk_trunc");
|
||||
} else {
|
||||
ret_val = c.LLVMBuildSExt(self.builder, result, ret_llvm, "thunk_sext");
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = c.LLVMBuildRet(self.builder, ret_val);
|
||||
}
|
||||
|
||||
// Restore position
|
||||
|
||||
Reference in New Issue
Block a user