authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-19 23:59:00-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-19 23:59:00-04:00
logaf4ccf34c1aac9c9914aa9cd5f3c857b7b90615d
tree429915d307f426bae5a1ee6cda30914e24ddad35
parentf7786d0ca822baee75b2d9fd2f3ef4f24421b2a9

ir: render keyword parameters


1 files changed, 43 insertions(+), 6 deletions(-)

src-self-hosted/ir.zig+43-6
...@@ -218,24 +218,54 @@ pub const Tree = struct {...@@ -218,24 +218,54 @@ pub const Tree = struct {
218 }218 }
219 const Positionals = @TypeOf(inst.positionals);219 const Positionals = @TypeOf(inst.positionals);
220 try stream.writeAll("= " ++ @tagName(inst_tag) ++ "(");220 try stream.writeAll("= " ++ @tagName(inst_tag) ++ "(");
221 inline for (@typeInfo(Positionals).Struct.fields) |arg_field, i| {221 const pos_fields = @typeInfo(Positionals).Struct.fields;
222 inline for (pos_fields) |arg_field, i| {
222 if (i != 0) {223 if (i != 0) {
223 try stream.writeAll(", ");224 try stream.writeAll(", ");
224 }225 }
225 try self.writeParamToStream(stream, @field(inst.positionals, arg_field.name), inst_table);226 try self.writeParamToStream(stream, @field(inst.positionals, arg_field.name), inst_table);
226 }227 }
228
229 comptime var need_comma = pos_fields.len != 0;
230 const KW_Args = @TypeOf(inst.kw_args);
231 inline for (@typeInfo(KW_Args).Struct.fields) |arg_field, i| {
232 if (need_comma) {
233 try stream.writeAll(",\n ");
234 }
235 if (@typeInfo(arg_field.field_type) == .Optional) {
236 if (@field(inst.kw_args, arg_field.name)) |non_optional| {
237 try stream.print("{}=", .{arg_field.name});
238 try self.writeParamToStream(stream, non_optional, inst_table);
239 need_comma = true;
240 }
241 } else {
242 try stream.print("{}=", .{arg_field.name});
243 try self.writeParamToStream(stream, @field(inst.kw_args, arg_field.name), inst_table);
244 need_comma = true;
245 }
246 }
247
227 try stream.writeByte(')');248 try stream.writeByte(')');
228 }249 }
229250
230 pub fn writeParamToStream(self: Tree, stream: var, param: var, inst_table: *const InstPtrTable) !void {251 fn writeParamToStream(self: Tree, stream: var, param: var, inst_table: *const InstPtrTable) !void {
252 if (@typeInfo(@TypeOf(param)) == .Enum) {
253 return stream.writeAll(@tagName(param));
254 }
231 switch (@TypeOf(param)) {255 switch (@TypeOf(param)) {
232 Value => {256 Value => {
233 try stream.print("{}", .{param});257 try stream.print("{}", .{param});
234 },258 },
235 *Inst => {259 *Inst => return self.writeInstParamToStream(stream, param, inst_table),
236 const info = inst_table.getValue(param).?;260 []*Inst => {
237 const prefix = if (info.fn_body == null) "@" else "%";261 try stream.writeByte('[');
238 try stream.print("{}{}", .{ prefix, info.index });262 for (param) |inst, i| {
263 if (i != 0) {
264 try stream.writeAll(", ");
265 }
266 try self.writeInstParamToStream(stream, inst, inst_table);
267 }
268 try stream.writeByte(']');
239 },269 },
240 Inst.Fn.Body => {270 Inst.Fn.Body => {
241 try stream.writeAll("{\n");271 try stream.writeAll("{\n");
...@@ -246,9 +276,16 @@ pub const Tree = struct {...@@ -246,9 +276,16 @@ pub const Tree = struct {
246 }276 }
247 try stream.writeByte('}');277 try stream.writeByte('}');
248 },278 },
279 bool => return stream.writeByte("01"[@boolToInt(param)]),
249 else => |T| @compileError("unimplemented: rendering parameter of type " ++ @typeName(T)),280 else => |T| @compileError("unimplemented: rendering parameter of type " ++ @typeName(T)),
250 }281 }
251 }282 }
283
284 fn writeInstParamToStream(self: Tree, stream: var, inst: *Inst, inst_table: *const InstPtrTable) !void {
285 const info = inst_table.getValue(inst).?;
286 const prefix = if (info.fn_body == null) "@" else "%";
287 try stream.print("{}{}", .{ prefix, info.index });
288 }
252};289};
253290
254const ParseContext = struct {291const ParseContext = struct {