-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmt.zig
More file actions
196 lines (184 loc) · 6.47 KB
/
fmt.zig
File metadata and controls
196 lines (184 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const std = @import("std");
const rtti = @import("root.zig");
const Type = rtti.Type;
const TypeRegistry = rtti.TypeRegistry;
const util = rtti.util;
pub fn formatType(
info: *const Type,
erased: *const anyopaque,
writer: *std.Io.Writer,
) anyerror!void {
switch (info.*) {
.bool => try formatBool(erased, writer),
.int => |*t| try formatInt(t, erased, writer),
.float => |*t| try formatFloat(t, erased, writer),
.pointer => |*t| try formatPointer(t, erased, writer),
.array => |*t| try formatArray(t.child, erased, t.len, writer),
.@"struct" => |*t| try formatStruct(t, erased, writer),
.optional => |*t| try formatOptional(t, erased, writer),
.@"enum" => |*t| try formatEnum(t, erased, writer),
.@"union" => |*t| try formatUnion(t, erased, writer),
.@"fn" => {
@panic("unimplemented");
},
}
}
pub fn formatBool(
erased: *const anyopaque,
writer: *std.Io.Writer,
) anyerror!void {
const bool_ptr: *const bool = @ptrCast(erased);
try writer.print("{}", .{bool_ptr.*});
}
pub fn formatInt(
info: *const Type.Int,
erased: *const anyopaque,
writer: *std.Io.Writer,
) anyerror!void {
const slice = util.sliceFromOpaque(u8, erased, info.size());
const int_types = @typeInfo(@TypeOf(util.integer_types)).@"struct".fields;
inline for (int_types) |int_type| {
const T: type = @field(util.integer_types, int_type.name);
const T_info = @typeInfo(T).int;
if (info.bits == T_info.bits and info.signedness == T_info.signedness and info.is_pointer_sized == util.isPointerSized(T)) {
const number = std.mem.bytesToValue(T, slice);
try writer.print("{d}", .{number});
break;
}
}
}
pub fn formatFloat(
info: *const Type.Float,
erased: *const anyopaque,
writer: *std.Io.Writer,
) anyerror!void {
const slice = util.sliceFromOpaque(u8, erased, info.size());
const float_types = @typeInfo(@TypeOf(util.float_types)).@"struct".fields;
inline for (float_types) |float_type| {
const T: type = @field(util.float_types, float_type.name);
const T_info = @typeInfo(T).float;
if (info.bits == T_info.bits) {
const number = std.mem.bytesToValue(T, slice);
try writer.print("{d}", .{number});
break;
}
}
}
pub fn formatPointer(
info: *const Type.Pointer,
erased: *const anyopaque,
writer: *std.Io.Writer,
) anyerror!void {
const slice = util.sliceFromOpaque(u8, erased, info.sizeInBytes());
const child_ptr: [*]const u8 = @ptrFromInt(std.mem.bytesToValue(usize, slice[0..8]));
switch (info.size) {
.one, .c => {
try writer.writeAll("*");
try formatType(info.child, child_ptr, writer);
},
.slice => {
const len = std.mem.bytesToValue(usize, slice[8..16]);
if (std.mem.eql(u8, info.child.typeName(), "u8")) {
// Interpret u8 slice as string
const string = util.sliceFromOpaque(u8, child_ptr, len);
try writer.print("\"{s}\"", .{string});
} else {
try formatArray(info.child, child_ptr, len, writer);
}
},
.many => {
@panic("unimplemented"); // depends on adding `sentinel_ptr` to `Type.Pointer`
},
}
}
pub fn formatArray(
child_type: *const Type,
ptr: *const anyopaque,
len: usize,
writer: *std.Io.Writer,
) anyerror!void {
const array_ptr: [*]const u8 = @ptrCast(ptr);
const elem_size = child_type.size();
const array_end = len * elem_size;
var i: usize = 0;
try writer.writeAll("{");
while (i < array_end) {
try formatType(child_type, array_ptr + i, writer);
if (i < (len - 1) * elem_size) try writer.writeAll(", ");
i += elem_size;
}
try writer.writeAll("}");
}
pub fn formatStruct(
info: *const Type.Struct,
erased: *const anyopaque,
writer: *std.Io.Writer,
) anyerror!void {
try writer.writeAll("{ ");
for (info.fields, 0..) |field_info, i| {
const field_ptr = info.getFieldPtrIndexed(erased, i);
const option_prefix = if (field_info.type.* == .optional) "?" else "";
try writer.print("{s}{s}: ", .{ field_info.name, option_prefix });
try formatType(field_info.type, field_ptr, writer);
if (i < info.fields.len - 1) try writer.writeAll(", ");
}
try writer.writeAll(" }");
}
pub fn formatOptional(
info: *const Type.Optional,
erased: *const anyopaque,
writer: *std.Io.Writer,
) anyerror!void {
const slice = util.sliceFromOpaque(u8, erased, info.size());
const option_offset: usize = info.size() / 2;
const is_some = slice[option_offset] != 0;
if (is_some) {
try formatType(info.child, erased, writer);
} else {
try writer.writeAll("null");
}
}
pub fn formatEnum(
info: *const Type.Enum,
erased: *const anyopaque,
writer: *std.Io.Writer,
) anyerror!void {
const size = info.size();
if (size > 8) @panic("enum tags greater than 64 bits unsupported");
const slice = util.sliceFromOpaque(u8, erased, size);
var value: u64 = 0;
std.mem.copyForwards(u8, std.mem.asBytes(&value), slice[0..size]); // TODO: test if works on big-endian
const variant = info.getNameFromValue(value).?;
try writer.print("{s}.{s}", .{ info.name, variant });
}
pub fn formatUnion(
info: *const Type.Union,
erased: *const anyopaque,
writer: *std.Io.Writer,
) anyerror!void {
const slice = util.sliceFromOpaque(u8, erased, info.size);
if (info.hasSafetyTag()) {
const tag_offset = info.size / 2;
var tag: usize = 0;
std.mem.copyForwards(u8, std.mem.asBytes(&tag), slice[tag_offset..]); // TODO: test if works on big-endian
const active_variant = info.fields[tag];
try writer.print("{s}.{s}", .{ info.name, active_variant.name });
if (active_variant.type) |field_type| {
try writer.writeAll("(");
try formatType(field_type, erased, writer);
try writer.writeAll(")");
}
} else {
try writer.print("{s}.unknown(", .{info.name});
try formatSliceAsHex(slice, writer);
try writer.writeAll(")");
}
}
pub fn formatSliceAsHex(slice: []const u8, writer: *std.Io.Writer) anyerror!void {
for (slice, 0..) |byte, i| {
try writer.print("{X:0>2}", .{byte});
if (i < slice.len - 1) {
try writer.writeByte(' ');
}
}
}