authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-19 01:06:15-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-19 19:31:50-04:00
log730dd887e49e8cde90af1dc76238c0c07d6c3c36
treea1f6d1385cbe990e3b00497afff542344e4f4831
parentbd37c8d8ed24f7e8887986ffcae67af30b9ce23c

ir: parse string literals as parameters


1 files changed, 18 insertions(+), 12 deletions(-)

src-self-hosted/ir.zig+18-12
......@@ -31,11 +31,6 @@ pub const Inst = struct {
3131 };
3232 }
3333
34 /// Represents a reference to another instruction.
35 pub const OtherInst = struct {
36 index: usize,
37 };
38
3934 /// This struct owns the `Value` memory. When the struct is deallocated,
4035 /// so is the `Value`. The value of a constant must be copied into
4136 /// a memory location for the value to survive after a const instruction.
......@@ -53,7 +48,7 @@ pub const Inst = struct {
5348 base: Inst = Inst{ .tag = .ptrtoint },
5449
5550 positionals: struct {
56 ptr: OtherInst,
51 ptr: *Inst,
5752 },
5853 kw_args: struct {},
5954 };
......@@ -61,7 +56,10 @@ pub const Inst = struct {
6156 pub const FieldPtr = struct {
6257 base: Inst = Inst{ .tag = .fieldptr },
6358
64 positionals: struct {},
59 positionals: struct {
60 object_ptr: *Inst,
61 field_name: *Inst,
62 },
6563 kw_args: struct {},
6664 };
6765
......@@ -257,8 +255,15 @@ fn parseInstructionGeneric(
257255 }
258256
259257 const Positionals = @TypeOf(inst_specific.positionals);
260 inline for (@typeInfo(Positionals).Struct.fields) |arg_field| {
258 inline for (@typeInfo(Positionals).Struct.fields) |arg_field, i| {
259 if (ctx.source[ctx.i] == ',') {
260 ctx.i += 1;
261 skipSpace(ctx);
262 } else if (ctx.source[ctx.i] == ')') {
263 return parseError(ctx, "expected positional parameter '{}'", .{arg_field.name});
264 }
261265 @field(inst_specific.positionals, arg_field.name) = try parseParameterGeneric(ctx, arg_field.field_type);
266 skipSpace(ctx);
262267 }
263268
264269 const KW_Args = @TypeOf(inst_specific.kw_args);
......@@ -297,16 +302,17 @@ fn parseParameterGeneric(ctx: *ParseContext, comptime T: type) !T {
297302 }
298303 switch (T) {
299304 Inst.Fn.Body => return parseBody(ctx),
300 Inst.OtherInst => {
305 *Inst => {
301306 const map = switch (ctx.source[ctx.i]) {
302307 '@' => ctx.global_name_map,
303 '%' => return parseError(ctx, "TODO implement OtherInst for %", .{}),
308 '%' => return parseError(ctx, "TODO implement parsing % parameter", .{}),
309 '"' => return parseStringLiteralConst(ctx, null),
304310 else => |byte| return parseError(ctx, "unexpected byte: '{c}'", .{byte}),
305311 };
306312 ctx.i += 1;
307313 const name_start = ctx.i;
308314 while (ctx.i < ctx.source.len) : (ctx.i += 1) switch (ctx.source[ctx.i]) {
309 ';', ' ', '\n', ',', ')' => break,
315 ' ', '\n', ',', ')' => break,
310316 else => continue,
311317 };
312318 const ident = ctx.source[name_start..ctx.i];
......@@ -315,7 +321,7 @@ fn parseParameterGeneric(ctx: *ParseContext, comptime T: type) !T {
315321 ctx.i = name_start - 1;
316322 return parseError(ctx, "unrecognized identifier: {}", .{bad_name});
317323 };
318 return Inst.OtherInst{ .index = kv.value };
324 return ctx.decls.items[kv.value];
319325 },
320326 Value => return parseError(ctx, "TODO implement parseParameterGeneric for type Value", .{}),
321327 else => @compileError("Unimplemented: ir parseParameterGeneric for type " ++ @typeName(T)),