Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
// PSEUDO-STRUCT.zig const std = @import("std"); const builtin = @import("builtin"); const debug = std.debug; const testing = std.testing; //The general technique employed here is to add fields to a struct by // using comptime to create a new struct type that includes the previous // type as a field. set/get/at etc are provided to access the recursively // embedded fields in a reasonable way. //used to mark the "bottom" of a type stack. const EndMarker = struct{}; pub fn Pseudo(comptime I: type, comptime Id: type, comptime equal: fn(Id,Id)bool) type { return struct { pub const Self = @This(); impl: I, pub const Field = I.Field; pub const init = I.init; pub fn count() comptime_int { comptime var n = usize(0); comptime var Curr = I; inline while(Curr != EndMarker):(n += 1) { Curr = std.meta.fieldInfo(Curr, "prev").field_type; } return n; } pub fn Ids() [count()]Id { comptime var ids: [count()]Id = undefined; comptime var index = usize(count() - 1); comptime var Curr = I; inline while(Curr != EndMarker):(index -= 1) { ids[index] = Curr.id; Curr = std.meta.fieldInfo(Curr, "prev").field_type; } return ids; } pub fn TypeOf(comptime i: Id) type { comptime var Curr = I; inline while(Curr != EndMarker) { const Value = std.meta.fieldInfo(Curr, "value").field_type; if(comptime equal(i, Curr.id)) return Value; Curr = std.meta.fieldInfo(Curr, "prev").field_type; } @compileError("invalid field id"); } fn fieldAt(self: var, comptime i: Id) *TypeOf(i) { const C = comptime std.meta.Child(@typeOf(self)); if(comptime equal(i, C.id)) return &self.value; return fieldAt(&self.prev, i); } pub fn at(self: *Self, comptime i: Id) *TypeOf(i) { return fieldAt(&self.impl, i); } fn fieldGet(self: var, comptime i: Id) TypeOf(i) { if(comptime equal(i, @typeOf(self).id)) return self.value; return fieldGet(self.prev, i); } pub fn get(self: Self, comptime i: Id) TypeOf(i) { return fieldGet(self.impl, i); } pub fn set(self: *Self, comptime i: Id, value: TypeOf(i)) void { self.at(i).* = value; } fn hasField(comptime i: Id) bool { comptime var Curr = I; inline while(Curr != EndMarker) { if(comptime equal(i, Curr.id)) return true; Curr = std.meta.fieldInfo(Curr, "prev").field_type; } return false; } }; } fn stringEqual(a: []const u8, b: []const u8) bool { return std.mem.eql(u8, a, b); } fn usizeEqual(a: usize, b: usize) bool { return a == b; } ///////STRUCT pub fn genStructFieldFn(comptime Prev: type, comptime Id: type, comptime equal: fn(Id,Id)bool) fn(type,Id)type { return struct { pub fn FieldFn(comptime Value: type, comptime field_id: Id) type { const Self = struct { const Self = @This(); const PseudoSelf = Pseudo(Self, Id, equal); prev: Prev, value: Value, pub const id = field_id; pub const Field = genStructFieldFn(Self, Id, equal); pub fn init(values: ...) PseudoSelf { const count = PseudoSelf.count(); if(values.len != count) @compileError("incorrect number of values for init"); var self = PseudoSelf(undefined); comptime var i = usize(values.len - 1); comptime var Curr = Self; inline while(i >= 0):(i -= 1) { self.set(Curr.id, values[i]); Curr = std.meta.fieldInfo(Curr, "prev").field_type; } return self; } pub fn add(self: Self, comptime i: Id, value: var) Self.Field(@typeOf(value), i) { const p_self = PseudoSelf{ .impl = self, }; const T = Self.Field(@typeOf(value), i); var s = T(undefined); inline for(comptime PseudoSelf.Ids()) |curr_id| s.set(curr_id, p_self.get(curr_id)); s.set(i, value); return s; } }; return Self.PseudoSelf; } }.FieldFn; } pub const Struct = struct { pub const Field = genStructFieldFn(EndMarker, []const u8, stringEqual); pub const impl = struct { pub fn add(comptime id: []const u8, value: var) Field(@typeOf(value), id) { var s = Field(@typeOf(value), id)(undefined); s.set(id, value); return s; } }; }; ///////UNION pub fn genUnionFieldFn(comptime Prev: type, comptime Id: type, comptime equal: fn(Id,Id)bool) fn(type,Id)type { return struct { pub fn FieldFn(comptime Value: type, comptime field_id: Id) type { const Self = extern union { const Self = @This(); const PseudoSelf = Pseudo(Self, Id, equal); prev: Prev, value: Value, pub const id = field_id; pub const Field = genUnionFieldFn(Self, Id, equal); pub fn init(comptime i: Id, value: PseudoSelf.TypeOf(i)) PseudoSelf { var self = PseudoSelf(undefined); self.set(i, value); return self; } }; return Self.PseudoSelf; } }.FieldFn; } pub const Union = struct { pub const Field = genUnionFieldFn(EndMarker, []const u8, stringEqual); }; ///////TUPLE pub fn genTupleFieldFn(comptime Prev: type) fn(type)type { return struct { pub fn FieldFn(comptime Value: type) type { const Self = struct { const Self = @This(); const PseudoSelf = Pseudo(Self, usize, usizeEqual); prev: Prev, value: Value, pub const id = if(Prev == EndMarker) 0 else Prev.id + 1; pub const Field = genTupleFieldFn(Self); pub fn init(values: ...) PseudoSelf { const count = PseudoSelf.count(); if(values.len != count) @compileError("incorrect number of values for init"); var self = PseudoSelf(undefined); comptime var i = usize(values.len - 1); comptime var Curr = Self; inline while(i >= 0):(i -= 1) { self.set(Curr.id, values[i]); Curr = std.meta.fieldInfo(Curr, "prev").field_type; } return self; } pub fn add(self: Self, value: var) Self.Field(@typeOf(value)) { const p_self = PseudoSelf{ .impl = self, }; const T = Self.Field(@typeOf(value)); var s = T(undefined); inline for(comptime PseudoSelf.Ids()) |curr_id| s.set(curr_id, p_self.get(curr_id)); s.set(T.count() - 1, value); return s; } }; return Self.PseudoSelf; } }.FieldFn; } pub const Tuple = struct { pub const Field = genTupleFieldFn(EndMarker); pub const impl = struct { pub fn add(value: var) Field(@typeOf(value)) { var s = Field(@typeOf(value))(undefined); s.set(0, value); return s; } }; }; ///////TaggedUnion pub fn genTaggedUnionFieldFn(comptime Prev: type, comptime Id: type, comptime equal: fn(Id,Id)bool) fn(type,Id)type { return struct { pub fn FieldFn(comptime Value: type, comptime field_id: Id) type { return struct { const Self = @This(); active: Id, inner: InnerUnion, const PrevInner = if(Prev != EndMarker) @typeOf(Prev(undefined).inner) else Union; const InnerUnion = PrevInner.Field(Value, field_id); pub const Field = genTaggedUnionFieldFn(Self, Id, equal); pub const TypeOf = InnerUnion.TypeOf; pub const count = InnerUnion.count; pub const Ids = InnerUnion.Ids; pub fn init(comptime i: Id, value: InnerUnion.TypeOf(i)) Self { var self = Self(undefined); self.active = i; self.set(i, value); return self; } pub fn at(self: *Self, comptime i: Id) *InnerUnion.TypeOf(i) { if(equal(self.active, i)) return self.inner.at(i); @panic("Attempt to set innactive tag"); } pub fn set(self: *Self, comptime i: Id, value: InnerUnion.TypeOf(i)) void { self.at(i).* = value; } pub fn get(self: Self, comptime i: Id) InnerUnion.TypeOf(i) { if(equal(self.active, i)) return self.inner.get(i); @panic("Attempt to get innactive tag"); } }; } }.FieldFn; } const TaggedUnion = struct { pub const Field = genTaggedUnionFieldFn(EndMarker, []const u8, stringEqual); }; ////TESTS test "Struct" { const S = Struct .Field(u8, "u8") .Field(u10, "u10") .Field(i16, "") .Field(void, "void") .Field([]const u8, "str"); testing.expect(S.TypeOf("u8") == u8); testing.expect(S.TypeOf("u10") == u10); testing.expect(S.TypeOf("") == i16); testing.expect(S.TypeOf("void") == void); testing.expect(S.TypeOf("str") == []const u8); var s = S.init(u8(232), u10(530), i16(14000), {}, "Test"); testing.expect(s.get("u8") == 232); testing.expect(s.get("u10") == 530); testing.expect(s.get("") == 14000); testing.expect(s.get("void") == {}); testing.expect(std.mem.eql(u8, s.get("str"), "Test")); s.set("u10", std.math.maxInt(u10)); testing.expect(s.at("u10").* == std.math.maxInt(u10)); //for(S.Ids()) |id| debug.warn("{},", id); } test "Struct.add" { var s = Struct .impl.add("u32", u32(8675)) .impl.add("[4]u8","TEST") .impl.add("f32", f32(1.8)) .impl.add("void", void{}); const S = @typeOf(s); testing.expect(S.TypeOf("u32") == u32); testing.expect(s.get("u32") == 8675); testing.expect(S.TypeOf("[4]u8") == [4]u8); testing.expect(std.mem.eql(u8, s.get("[4]u8"), "TEST")); testing.expect(S.TypeOf("f32") == f32); testing.expect(s.get("f32") == 1.8); testing.expect(S.TypeOf("void") == void); testing.expect(s.get("void") == {}); } test "Tuple" { const T = Tuple .Field(u8) .Field(u10) .Field(i16) .Field(void) .Field([]const u8); testing.expect(T.TypeOf(0) == u8); testing.expect(T.TypeOf(1) == u10); testing.expect(T.TypeOf(2) == i16); testing.expect(T.TypeOf(3) == void); testing.expect(T.TypeOf(4) == []const u8); var t = T.init(u8(232), u10(530), i16(14000), {}, "Test"); testing.expect(t.get(0) == 232); testing.expect(t.get(1) == 530); testing.expect(t.get(2) == 14000); testing.expect(t.get(3) == {}); testing.expect(std.mem.eql(u8, t.get(4), "Test")); t.set(1, std.math.maxInt(u10)); testing.expect(t.at(1).* == std.math.maxInt(u10)); //for(T.Ids()) |id| debug.warn("{},", id); } test "Tuple.add" { var t = Tuple .impl.add(u32(8675)) .impl.add("TEST") .impl.add(f32(1.8)) .impl.add(void{}); const T = @typeOf(t); testing.expect(T.TypeOf(0) == u32); testing.expect(t.get(0) == 8675); testing.expect(T.TypeOf(1) == [4]u8); testing.expect(std.mem.eql(u8, t.get(1), "TEST")); testing.expect(T.TypeOf(2) == f32); testing.expect(t.get(2) == 1.8); testing.expect(T.TypeOf(3) == void); testing.expect(t.get(3) == {}); } test "Union" { const U = Union .Field(u8, "u8") .Field(u10, "u10") .Field(i16, "") .Field(void, "void") .Field([]const u8, "str"); testing.expect(U.TypeOf("u8") == u8); testing.expect(U.TypeOf("u10") == u10); testing.expect(U.TypeOf("") == i16); testing.expect(U.TypeOf("void") == void); testing.expect(U.TypeOf("str") == []const u8); var u = U.init("u8", 232); testing.expect(u.get("u8") == 232); u.set("u10", 530); testing.expect(u.get("u10") == 530); u.set("", 14000); testing.expect(u.at("").* == 14000); u.set("str", "Test"); testing.expect(u.get("void") == {}); testing.expect(std.mem.eql(u8, u.get("str"), "Test")); testing.expect(@sizeOf(U) == @sizeOf([]const u8)); //for(U.Ids()) |id| debug.warn("{},", id); } test "TaggedUnion" { const U = TaggedUnion .Field(u8, "u8") .Field(u10, "u10") .Field(i16, "") .Field(void, "void") .Field([]const u8, "str"); testing.expect(U.TypeOf("u8") == u8); testing.expect(U.TypeOf("u10") == u10); testing.expect(U.TypeOf("") == i16); testing.expect(U.TypeOf("void") == void); testing.expect(U.TypeOf("str") == []const u8); var u = U.init("u8", 232); testing.expect(u.get("u8") == 232); u = U.init("u10", 530); testing.expect(u.get("u10") == 530); u = U.init("", 14000); testing.expect(u.at("").* == 14000); u = U.init("str", "Test"); testing.expect(std.mem.eql(u8, u.get("str"), "Test")); u.set("str", "Tagged"); testing.expect(std.mem.eql(u8, u.get("str"), "Tagged")); //active is a slice, the largest of the unioned values is also testing.expect(@sizeOf(U) == @sizeOf([]const u8) * 2); //for(U.Ids()) |id| debug.warn("{},", id); } // AOSSOA.zig //pseudo_struct.zig: http://zig.tgschultz.com/pseudo_struct.zig const PseudoStruct = @import("pseduo_struct.zig").Struct; pub fn AosSoa(comptime layout: enum{Aos, Soa}, comptime length: usize, comptime T: type) type { return struct { const Self = @This(); inner: Inner, const Inner = switch(layout) { .Aos => [length]T, .Soa => comptime blk: { var S = PseudoStruct; inline for(std.meta.fields(T)) |field| { S = S.Field([length]field.field_type, field.name); } break :blk S; }, }; pub fn TypeOf(comptime field: []const u8) type { return switch(layout) { .Aos => @typeOf(@field(T(undefined), field)), .Soa => Inner.TypeOf(field).Child, }; } pub fn set(self: *Self, idx: usize, value: T) void { switch(layout) { .Aos => self.inner[idx] = value, .Soa => { inline for(std.meta.fields(T)) |field| { self.inner.at(field.name)[idx] = @field(value, field.name); } }, } } pub fn init(self: *Self, value: [length]T) Self { var self = Self(undefined); for(value) |v, i| self.set(i, v); } pub fn get(self: *Self, idx: usize) T { switch(layout) { .Aos => return self.inner[idx], .Soa => { var value = T(undefined); inline for(std.meta.fields(T)) |field| { @field(value, field.name) = self.inner.at(field.name)[idx]; } return value; }, } } pub fn setField(self: *Self, idx: usize, comptime field: []const u8, value: TypeOf(field)) void { switch(layout) { .Aos => @field(self.inner[idx], field) = value, .Soa => self.inner.at(field)[idx] = value, } } pub fn getField(self: *Self, idx: usize, comptime field: []const u8) TypeOf(field) { return switch(layout) { .Aos => @field(self.inner[idx], field), .Soa => self.inner.at(field)[idx], }; } }; } const MyStruct = struct { a: void, b: u32, c: ?*MyStruct, d: [10]u8, e: f16, }; const MyAosSoa = AosSoa(.Aos, 10, MyStruct); export fn foo(idx: usize) u32 { var list = MyAosSoa(undefined); list.set(3, MyStruct { .a = {}, .b = 8675309, .c = null, .d = "0123456789", .e = 1.5, }); list.setField(2, "e", 123.45); const item = list.get(2); testing.expect(item.e == 123.45); return list.getField(idx, "b"); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree