From 5b99c4992a2607f5f109da917b0a261c9db213b6 Mon Sep 17 00:00:00 2001 From: Christofer Nolander Date: Wed, 23 Aug 2023 20:00:15 +0200 Subject: [PATCH 1/3] format bit flags as set This changes to format output from: MyFlagType{ .first_bit = true, second_bit = false, _reserved_bit_0 = false, _reserved_bit_1 = false } to the following: MyFlagType{ first_bit } That is, we only show bits which are `true`. --- generator/vulkan/render.zig | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/generator/vulkan/render.zig b/generator/vulkan/render.zig index c4df606..840ac08 100644 --- a/generator/vulkan/render.zig +++ b/generator/vulkan/render.zig @@ -49,6 +49,7 @@ const preamble = \\ pub fn contains(lhs: FlagsType, rhs: FlagsType) bool { \\ return toInt(intersect(lhs, rhs)) == toInt(rhs); \\ } +++ bit_flag_format ++ \\ }; \\} \\pub fn makeApiVersion(variant: u3, major: u7, minor: u10, patch: u12) u32 { @@ -69,6 +70,31 @@ const preamble = \\ ; +const bit_flag_format = + \\ pub fn format( + \\ self: @This(), + \\ comptime _: []const u8, + \\ _: std.fmt.FormatOptions, + \\ writer: anytype, + \\ ) !void { + \\ try writer.writeAll(@typeName(@This()) ++ "{"); + \\ var first = true; + \\ inline for (comptime std.meta.fieldNames(@This())) |name| { + \\ if (name[0] == '_') continue; + \\ if (@field(self, name)) { + \\ if (first) { + \\ try writer.writeAll(" " ++ name); + \\ first = false; + \\ } else { + \\ try writer.writeAll(", " ++ name); + \\ } + \\ } + \\ } + \\ if (!first) try writer.writeAll(" "); + \\ try writer.writeAll("}"); + \\ } +; + const builtin_types = std.ComptimeStringMap([]const u8, .{ .{ "void", @typeName(void) }, .{ "char", @typeName(u8) }, @@ -1060,6 +1086,7 @@ fn Renderer(comptime WriterType: type) type { \\ } \\ return true; \\ } + ++ bit_flag_format ++ \\ }; \\} \\ From 22874125360103f5c830712eeb55b7054250f485 Mon Sep 17 00:00:00 2001 From: Christofer Nolander Date: Wed, 23 Aug 2023 22:12:17 +0200 Subject: [PATCH 2/3] use format mixin for bit-flags --- generator/vulkan/render.zig | 49 +++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/generator/vulkan/render.zig b/generator/vulkan/render.zig index 840ac08..a6821af 100644 --- a/generator/vulkan/render.zig +++ b/generator/vulkan/render.zig @@ -49,37 +49,20 @@ const preamble = \\ pub fn contains(lhs: FlagsType, rhs: FlagsType) bool { \\ return toInt(intersect(lhs, rhs)) == toInt(rhs); \\ } -++ bit_flag_format ++ + \\ pub usingnamespace FlagFormatMixin(FlagsType); \\ }; \\} - \\pub fn makeApiVersion(variant: u3, major: u7, minor: u10, patch: u12) u32 { - \\ return (@as(u32, variant) << 29) | (@as(u32, major) << 22) | (@as(u32, minor) << 12) | patch; - \\} - \\pub fn apiVersionVariant(version: u32) u3 { - \\ return @truncate(u3, version >> 29); - \\} - \\pub fn apiVersionMajor(version: u32) u7 { - \\ return @truncate(u7, version >> 22); - \\} - \\pub fn apiVersionMinor(version: u32) u10 { - \\ return @truncate(u10, version >> 12); - \\} - \\pub fn apiVersionPatch(version: u32) u12 { - \\ return @truncate(u12, version); - \\} - \\ -; - -const bit_flag_format = + \\fn FlagFormatMixin(comptime FlagsType: type) type { + \\ return struct { \\ pub fn format( - \\ self: @This(), + \\ self: FlagsType, \\ comptime _: []const u8, \\ _: std.fmt.FormatOptions, \\ writer: anytype, \\ ) !void { - \\ try writer.writeAll(@typeName(@This()) ++ "{"); + \\ try writer.writeAll(@typeName(FlagsType) ++ "{"); \\ var first = true; - \\ inline for (comptime std.meta.fieldNames(@This())) |name| { + \\ inline for (comptime std.meta.fieldNames(FlagsType)) |name| { \\ if (name[0] == '_') continue; \\ if (@field(self, name)) { \\ if (first) { @@ -93,6 +76,24 @@ const bit_flag_format = \\ if (!first) try writer.writeAll(" "); \\ try writer.writeAll("}"); \\ } + \\ }; + \\} + \\pub fn makeApiVersion(variant: u3, major: u7, minor: u10, patch: u12) u32 { + \\ return (@as(u32, variant) << 29) | (@as(u32, major) << 22) | (@as(u32, minor) << 12) | patch; + \\} + \\pub fn apiVersionVariant(version: u32) u3 { + \\ return @truncate(u3, version >> 29); + \\} + \\pub fn apiVersionMajor(version: u32) u7 { + \\ return @truncate(u7, version >> 22); + \\} + \\pub fn apiVersionMinor(version: u32) u10 { + \\ return @truncate(u10, version >> 12); + \\} + \\pub fn apiVersionPatch(version: u32) u12 { + \\ return @truncate(u12, version); + \\} + \\ ; const builtin_types = std.ComptimeStringMap([]const u8, .{ @@ -1086,7 +1087,7 @@ fn Renderer(comptime WriterType: type) type { \\ } \\ return true; \\ } - ++ bit_flag_format ++ + \\ pub usingnamespace FlagFormatMixin(CommandFlags); \\ }; \\} \\ From 190a4a054d19c12d5f447174e377423f6c6b1f24 Mon Sep 17 00:00:00 2001 From: Christofer Nolander Date: Wed, 23 Aug 2023 22:17:05 +0200 Subject: [PATCH 3/3] add leading dot for bit flags format --- generator/vulkan/render.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generator/vulkan/render.zig b/generator/vulkan/render.zig index a6821af..ca2e454 100644 --- a/generator/vulkan/render.zig +++ b/generator/vulkan/render.zig @@ -66,10 +66,10 @@ const preamble = \\ if (name[0] == '_') continue; \\ if (@field(self, name)) { \\ if (first) { - \\ try writer.writeAll(" " ++ name); + \\ try writer.writeAll(" ." ++ name); \\ first = false; \\ } else { - \\ try writer.writeAll(", " ++ name); + \\ try writer.writeAll(", ." ++ name); \\ } \\ } \\ }