This commit is contained in:
agra
2026-02-16 01:58:30 +02:00
parent b20676375d
commit c8ceceed0f
6 changed files with 104 additions and 236 deletions

View File

@@ -375,6 +375,13 @@ pub const Type = union(enum) {
return false;
}
fn fmtAlloc(allocator: std.mem.Allocator, comptime fmt: []const u8, args: anytype) ![]const u8 {
var buf: [128]u8 = undefined;
const result = std.fmt.bufPrint(&buf, fmt, args) catch
return try std.fmt.allocPrint(allocator, fmt, args);
return try allocator.dupe(u8, result);
}
/// Format type name for mangling and display (e.g. "s32", "u8", "f64")
pub fn displayName(self: Type, allocator: std.mem.Allocator) ![]const u8 {
return switch (self) {
@@ -397,66 +404,11 @@ pub const Type = union(enum) {
.enum_type => |name| name,
.struct_type => |name| name,
.union_type => |name| name,
.slice_type => |info| {
var buf: [128]u8 = undefined;
const result = std.fmt.bufPrint(&buf, "[]{s}", .{info.element_name}) catch {
// Fall back to dynamic allocation for very long element names
var dyn = std.ArrayList(u8).empty;
try dyn.appendSlice(allocator, "[]");
try dyn.appendSlice(allocator, info.element_name);
return try dyn.toOwnedSlice(allocator);
};
return try allocator.dupe(u8, result);
},
.pointer_type => |info| {
var buf: [128]u8 = undefined;
const result = std.fmt.bufPrint(&buf, "*{s}", .{info.pointee_name}) catch {
var dyn = std.ArrayList(u8).empty;
try dyn.appendSlice(allocator, "*");
try dyn.appendSlice(allocator, info.pointee_name);
return try dyn.toOwnedSlice(allocator);
};
return try allocator.dupe(u8, result);
},
.many_pointer_type => |info| {
var buf: [128]u8 = undefined;
const result = std.fmt.bufPrint(&buf, "[*]{s}", .{info.element_name}) catch {
var dyn = std.ArrayList(u8).empty;
try dyn.appendSlice(allocator, "[*]");
try dyn.appendSlice(allocator, info.element_name);
return try dyn.toOwnedSlice(allocator);
};
return try allocator.dupe(u8, result);
},
.array_type => |info| {
var buf: [128]u8 = undefined;
const result = std.fmt.bufPrint(&buf, "[{d}]{s}", .{ info.length, info.element_name }) catch {
var dyn = std.ArrayList(u8).empty;
try dyn.appendSlice(allocator, "[");
var tmp: [10]u8 = undefined;
const len_str = std.fmt.bufPrint(&tmp, "{d}", .{info.length}) catch unreachable;
try dyn.appendSlice(allocator, len_str);
try dyn.appendSlice(allocator, "]");
try dyn.appendSlice(allocator, info.element_name);
return try dyn.toOwnedSlice(allocator);
};
return try allocator.dupe(u8, result);
},
.vector_type => |info| {
var buf: [128]u8 = undefined;
const result = std.fmt.bufPrint(&buf, "Vector({d},{s})", .{ info.length, info.element_name }) catch {
var dyn = std.ArrayList(u8).empty;
try dyn.appendSlice(allocator, "Vector(");
var tmp: [10]u8 = undefined;
const len_str = std.fmt.bufPrint(&tmp, "{d}", .{info.length}) catch unreachable;
try dyn.appendSlice(allocator, len_str);
try dyn.appendSlice(allocator, ",");
try dyn.appendSlice(allocator, info.element_name);
try dyn.appendSlice(allocator, ")");
return try dyn.toOwnedSlice(allocator);
};
return try allocator.dupe(u8, result);
},
.slice_type => |info| return fmtAlloc(allocator, "[]{s}", .{info.element_name}),
.pointer_type => |info| return fmtAlloc(allocator, "*{s}", .{info.pointee_name}),
.many_pointer_type => |info| return fmtAlloc(allocator, "[*]{s}", .{info.element_name}),
.array_type => |info| return fmtAlloc(allocator, "[{d}]{s}", .{ info.length, info.element_name }),
.vector_type => |info| return fmtAlloc(allocator, "Vector({d},{s})", .{ info.length, info.element_name }),
.function_type => |info| {
var buf = std.ArrayList(u8).empty;
try buf.append(allocator, '(');