auto type erasure for protocols & struct literal init bloc
This commit is contained in:
@@ -306,6 +306,7 @@ pub const StructLiteral = struct {
|
||||
struct_name: ?[]const u8, // null for anonymous `.{ ... }`
|
||||
type_expr: ?*Node = null, // for GenericType(args).{ ... }
|
||||
field_inits: []const StructFieldInit,
|
||||
init_block: ?*Node = null, // optional `{ stmts }` block after struct literal
|
||||
};
|
||||
|
||||
pub const Lambda = struct {
|
||||
|
||||
@@ -5933,6 +5933,22 @@ pub const CodeGen = struct {
|
||||
}
|
||||
}
|
||||
|
||||
// Init block: T.{ ... } { self.method(); }
|
||||
if (sl.init_block) |ib| {
|
||||
if (ib.data == .block) {
|
||||
try self.pushScope();
|
||||
const self_ty = Type{ .pointer_type = .{ .pointee_name = sname } };
|
||||
// self is a *T — store the struct alloca address into a ptr-sized alloca
|
||||
const self_alloca = self.buildEntryBlockAlloca(self.ptrType(), "self");
|
||||
_ = c.LLVMBuildStore(self.builder, alloca, self_alloca);
|
||||
try self.registerVariable("self", self_alloca, self_ty);
|
||||
for (ib.data.block.stmts) |stmt| {
|
||||
_ = try self.genStmt(stmt);
|
||||
}
|
||||
try self.popScope();
|
||||
}
|
||||
}
|
||||
|
||||
return alloca;
|
||||
}
|
||||
|
||||
@@ -6213,6 +6229,29 @@ pub const CodeGen = struct {
|
||||
}
|
||||
}
|
||||
|
||||
// Struct literal targeting protocol type: auto type erasure
|
||||
if (node.data == .struct_literal and target_ty.isStruct()) {
|
||||
if (self.protocol_decls.get(target_ty.struct_type)) |pd| {
|
||||
const sl = node.data.struct_literal;
|
||||
const concrete_name = sl.struct_name orelse if (sl.type_expr) |te| blk: {
|
||||
const ty = self.resolveType(te);
|
||||
if (ty.isStruct()) break :blk ty.struct_type;
|
||||
break :blk null;
|
||||
} else null;
|
||||
if (concrete_name) |cn| {
|
||||
const key = try std.fmt.allocPrint(self.allocator, "{s}\x00{s}", .{ pd.name, cn });
|
||||
if (self.impl_blocks.contains(key)) {
|
||||
const alloca = try self.genStructLiteral(sl, cn);
|
||||
const sname = self.resolveAlias(cn);
|
||||
if (self.lookupStructInfo(sname)) |si| {
|
||||
const loaded = c.LLVMBuildLoad2(self.builder, si.llvm_type.?, alloca, "auto_erase_load");
|
||||
return self.buildProtocolValue(loaded, Type{ .struct_type = cn }, pd) catch return loaded;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Struct literal targeting struct type: pass struct name context
|
||||
if (node.data == .struct_literal and target_ty.isStruct()) {
|
||||
const alloca = try self.genStructLiteral(node.data.struct_literal, target_ty.struct_type);
|
||||
@@ -6369,6 +6408,26 @@ pub const CodeGen = struct {
|
||||
}
|
||||
}
|
||||
|
||||
// Auto type erasure: concrete type → protocol (implicit xx)
|
||||
if (target_ty.isStruct()) {
|
||||
if (self.protocol_decls.get(target_ty.struct_type)) |pd| {
|
||||
const concrete_name = if (src_ty.isStruct())
|
||||
src_ty.struct_type
|
||||
else if (src_ty.isPointer())
|
||||
src_ty.pointer_type.pointee_name
|
||||
else if (src_ty.isManyPointer())
|
||||
src_ty.many_pointer_type.element_name
|
||||
else
|
||||
null;
|
||||
if (concrete_name) |cn| {
|
||||
const key = try std.fmt.allocPrint(self.allocator, "{s}\x00{s}", .{ pd.name, cn });
|
||||
if (self.impl_blocks.contains(key)) {
|
||||
return self.buildProtocolValue(val, src_ty, pd) catch return val;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Scalar to vector broadcast
|
||||
if (target_ty.isVector() and !src_ty.isVector()) {
|
||||
const elem_ty = target_ty.vectorElementType() orelse return self.emitError("cannot determine vector element type");
|
||||
|
||||
@@ -1034,10 +1034,17 @@ pub const Parser = struct {
|
||||
}
|
||||
try self.expect(.r_brace);
|
||||
|
||||
// Optional init block: T.{ fields } { stmts }
|
||||
const init_block: ?*Node = if (self.current.tag == .l_brace)
|
||||
try self.parseBlock()
|
||||
else
|
||||
null;
|
||||
|
||||
return try self.createNode(start_pos, .{ .struct_literal = .{
|
||||
.struct_name = struct_name,
|
||||
.type_expr = type_expr,
|
||||
.field_inits = try field_inits.toOwnedSlice(self.allocator),
|
||||
.init_block = init_block,
|
||||
} });
|
||||
}
|
||||
|
||||
@@ -2017,7 +2024,17 @@ pub const Parser = struct {
|
||||
self.advance(); // skip 'push'
|
||||
|
||||
const context_expr = try self.parseExpr();
|
||||
const body = try self.parseBlock();
|
||||
|
||||
// push Context.{ ... } { body } — if parseExpr consumed the push body
|
||||
// as a struct init block, steal it back as the push body.
|
||||
// (if/while don't have this issue — they require bool/optional conditions)
|
||||
const body = if (context_expr.data == .struct_literal and
|
||||
context_expr.data.struct_literal.init_block != null)
|
||||
body_blk: {
|
||||
const ib = context_expr.data.struct_literal.init_block.?;
|
||||
context_expr.data.struct_literal.init_block = null;
|
||||
break :body_blk ib;
|
||||
} else try self.parseBlock();
|
||||
|
||||
return try self.createNode(start, .{ .push_stmt = .{
|
||||
.context_expr = context_expr,
|
||||
|
||||
Reference in New Issue
Block a user