From d6f0a309482542a874bb0be86a677d83b49f137e Mon Sep 17 00:00:00 2001 From: dimgigov Date: Wed, 10 Jun 2026 13:25:11 +0300 Subject: [PATCH] selfhost: add *_Drop wrappers to stdlib types; auto-drop uses _Drop instead of _Free - Array.bux, Channel.bux, Set.bux, Map.bux: add *_Drop delegating to *_Free - hir_lower.bux: Lcx_BuildAutoDropFree searches for _Drop suffix instead of _Free - Skip direct lowering of generic functions (only monomorphized instances) --- lib/Array.bux | 4 ++++ lib/Channel.bux | 4 ++++ lib/Map.bux | 4 ++++ lib/Set.bux | 4 ++++ src/hir_lower.bux | 27 ++++++++++++++++++++++----- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/Array.bux b/lib/Array.bux index 2dbfb4e..3b5c348 100644 --- a/lib/Array.bux +++ b/lib/Array.bux @@ -46,4 +46,8 @@ func Array_Free(self: *Array) { self.cap = 0; } +func Array_Drop(self: *Array) { + Array_Free(self); +} + } diff --git a/lib/Channel.bux b/lib/Channel.bux index bc31ecb..79e20a1 100644 --- a/lib/Channel.bux +++ b/lib/Channel.bux @@ -32,6 +32,10 @@ func Channel_Free(ch: *Channel) { bux_channel_free(ch.handle); } +func Channel_Drop(ch: *Channel) { + Channel_Free(ch); +} + /* Convenience wrappers for common types */ func Channel_SendInt(ch: *Channel, value: int) { bux_channel_send(ch.handle, (&value) as *void); diff --git a/lib/Map.bux b/lib/Map.bux index 650803e..ddbbb28 100644 --- a/lib/Map.bux +++ b/lib/Map.bux @@ -87,6 +87,10 @@ func Map_Free(m: *Map) { m.len = 0; } +func Map_Drop(m: *Map) { + Map_Free(m); +} + // --------------------------------------------------------------------------- // StringMap — specialized Map for String keys, using strcmp // --------------------------------------------------------------------------- diff --git a/lib/Set.bux b/lib/Set.bux index 6a8fca6..cf57fe2 100644 --- a/lib/Set.bux +++ b/lib/Set.bux @@ -68,4 +68,8 @@ func Set_Free(s: *Set) { s.len = 0; } +func Set_Drop(s: *Set) { + Set_Free(s); +} + } diff --git a/src/hir_lower.bux b/src/hir_lower.bux index e35deac..812a927 100644 --- a/src/hir_lower.bux +++ b/src/hir_lower.bux @@ -2104,11 +2104,16 @@ func Lcx_EvalConstExpr(ctx: *LowerCtx, expr: *Expr) -> int { func Lcx_BuildAutoDropFree(ctx: *LowerCtx, typeName: String) -> String { if String_StartsWith(typeName, "Array_") { let elemType: String = bux_str_slice(typeName, 6, bux_strlen(typeName) - 6); + // Ensure inner free is also monomorphized since Drop calls Free let genFree: *Decl = Lcx_FindGenericFunc(ctx, "Array_Free"); if genFree != null as *Decl { Lcx_GenerateFuncInstance(ctx, genFree, elemType, "", 1); } - return String_Concat("Array_Free_", elemType); + let genDrop: *Decl = Lcx_FindGenericFunc(ctx, "Array_Drop"); + if genDrop != null as *Decl { + Lcx_GenerateFuncInstance(ctx, genDrop, elemType, "", 1); + } + return String_Concat("Array_Drop_", elemType); } if String_StartsWith(typeName, "Channel_") { let elemType: String = bux_str_slice(typeName, 8, bux_strlen(typeName) - 8); @@ -2116,7 +2121,11 @@ func Lcx_BuildAutoDropFree(ctx: *LowerCtx, typeName: String) -> String { if genFree != null as *Decl { Lcx_GenerateFuncInstance(ctx, genFree, elemType, "", 1); } - return String_Concat("Channel_Free_", elemType); + let genDrop: *Decl = Lcx_FindGenericFunc(ctx, "Channel_Drop"); + if genDrop != null as *Decl { + Lcx_GenerateFuncInstance(ctx, genDrop, elemType, "", 1); + } + return String_Concat("Channel_Drop_", elemType); } if String_StartsWith(typeName, "Set_") { let elemType: String = bux_str_slice(typeName, 4, bux_strlen(typeName) - 4); @@ -2124,7 +2133,11 @@ func Lcx_BuildAutoDropFree(ctx: *LowerCtx, typeName: String) -> String { if genFree != null as *Decl { Lcx_GenerateFuncInstance(ctx, genFree, elemType, "", 1); } - return String_Concat("Set_Free_", elemType); + let genDrop: *Decl = Lcx_FindGenericFunc(ctx, "Set_Drop"); + if genDrop != null as *Decl { + Lcx_GenerateFuncInstance(ctx, genDrop, elemType, "", 1); + } + return String_Concat("Set_Drop_", elemType); } if String_StartsWith(typeName, "Map_") { let rest: String = bux_str_slice(typeName, 4, bux_strlen(typeName) - 4); @@ -2144,7 +2157,11 @@ func Lcx_BuildAutoDropFree(ctx: *LowerCtx, typeName: String) -> String { if genFree != null as *Decl { Lcx_GenerateFuncInstance(ctx, genFree, kType, vType, 2); } - return String_Concat(String_Concat("Map_Free_", kType), String_Concat("_", vType)); + let genDrop: *Decl = Lcx_FindGenericFunc(ctx, "Map_Drop"); + if genDrop != null as *Decl { + Lcx_GenerateFuncInstance(ctx, genDrop, kType, vType, 2); + } + return String_Concat(String_Concat("Map_Drop_", kType), String_Concat("_", vType)); } } return ""; @@ -2236,7 +2253,7 @@ func HirLower_LowerModule(mod: *Module, sema: *Sema) -> *HirModule { } hm.structCount = hm.structCount + 1; } - if decl.kind == dkFunc && decl.refBody != null as *Block { + if decl.kind == dkFunc && decl.refBody != null as *Block && decl.typeParamCount == 0 { let f: *HirFunc = Lcx_LowerFunc(ctx, decl); ctx.funcs[ctx.funcCount] = *f; ctx.funcCount = ctx.funcCount + 1;