authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-19 14:25:47+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-19 16:12:29-07:00
logab8a9a6605900fc0b064c321504ac4e7dc1ca6f4
tree46b5cd9da18d8df31b60756eab2347de1b62896b
parent338a495648f07a2734e012034fd301a0e77f8202

stage2: fix astgen of decl ref, add test for global consts


4 files changed, 42 insertions(+), 9 deletions(-)

src-self-hosted/astgen.zig+8-6
......@@ -1223,9 +1223,10 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo
12231223 }
12241224
12251225 if (mod.lookupDeclName(scope, ident_name)) |decl| {
1226 // TODO handle lvalues
12271226 const result = try addZIRInst(mod, scope, src, zir.Inst.DeclValInModule, .{ .decl = decl }, .{});
1228 return rlWrap(mod, scope, rl, result);
1227 if (rl == .lvalue or rl == .ref)
1228 return result;
1229 return rlWrap(mod, scope, rl, try addZIRUnOp(mod, scope, src, .deref, result));
12291230 }
12301231
12311232 return mod.failNode(scope, &ident.base, "use of undeclared identifier '{}'", .{ident_name});
......@@ -1258,7 +1259,8 @@ fn multilineStrLiteral(mod: *Module, scope: *Scope, node: *ast.Node.MultilineStr
12581259 // line lengths and new lines
12591260 var len = lines.len - 1;
12601261 for (lines) |line| {
1261 len += tree.tokenSlice(line).len - 2;
1262 // 2 for the '//' + 1 for '\n'
1263 len += tree.tokenSlice(line).len - 3;
12621264 }
12631265
12641266 const bytes = try scope.arena().alloc(u8, len);
......@@ -1268,9 +1270,9 @@ fn multilineStrLiteral(mod: *Module, scope: *Scope, node: *ast.Node.MultilineStr
12681270 bytes[i] = '\n';
12691271 i += 1;
12701272 }
1271 const slice = tree.tokenSlice(line)[2..];
1272 mem.copy(u8, bytes[i..], slice);
1273 i += slice.len;
1273 const slice = tree.tokenSlice(line);
1274 mem.copy(u8, bytes[i..], slice[2..slice.len - 1]);
1275 i += slice.len - 3;
12741276 }
12751277
12761278 return addZIRInst(mod, scope, src, zir.Inst.Str, .{ .bytes = bytes }, .{});
src-self-hosted/type.zig+2-1
......@@ -457,7 +457,8 @@ pub const Type = extern union {
457457 try param_type.format("", .{}, out_stream);
458458 }
459459 try out_stream.writeAll(") ");
460 try payload.return_type.format("", .{}, out_stream);
460 ty = payload.return_type;
461 continue;
461462 },
462463
463464 .array_u8 => {
src-self-hosted/zir_sema.zig+1-2
......@@ -581,8 +581,7 @@ fn analyzeInstDeclVal(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) Inne
581581
582582fn analyzeInstDeclValInModule(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclValInModule) InnerError!*Inst {
583583 const decl = inst.positionals.decl;
584 const ptr = try mod.analyzeDeclRef(scope, inst.base.src, decl);
585 return mod.analyzeDeref(scope, inst.base.src, ptr, inst.base.src);
584 return mod.analyzeDeclRef(scope, inst.base.src, decl);
586585}
587586
588587fn analyzeInstCall(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst {
test/stage2/compare_output.zig+31
......@@ -617,6 +617,37 @@ pub fn addCases(ctx: *TestContext) !void {
617617 ,
618618 "",
619619 );
620
621 case.addCompareOutput(
622 \\export fn _start() noreturn {
623 \\ add(aa, bb);
624 \\
625 \\ exit();
626 \\}
627 \\
628 \\const aa = 'ぁ';
629 \\const bb = '\x03';
630 \\
631 \\fn add(a: u32, b: u32) void {
632 \\ assert(a + b == 12356);
633 \\}
634 \\
635 \\pub fn assert(ok: bool) void {
636 \\ if (!ok) unreachable; // assertion failure
637 \\}
638 \\
639 \\fn exit() noreturn {
640 \\ asm volatile ("syscall"
641 \\ :
642 \\ : [number] "{rax}" (231),
643 \\ [arg1] "{rdi}" (0)
644 \\ : "rcx", "r11", "memory"
645 \\ );
646 \\ unreachable;
647 \\}
648 ,
649 "",
650 );
620651 }
621652
622653 {