authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-22 14:54:13+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-22 14:54:13+01:00
log8111453cc12f7908110850cf64efedb9c69ede98
tree80c6774eda1c73f3b45e04989840f734756d7b19
parent4f3071a8172b1e5d0aa22c07471de2056df1608a
signature Commit is signed but in an unrecognized format.

astgen: implement array types


4 files changed, 47 insertions(+), 43 deletions(-)

lib/std/zig/ast.zig+5-4
......@@ -1430,7 +1430,7 @@ pub const Tree = struct {
14301430 .ast = .{
14311431 .lbracket = tree.nodes.items(.main_token)[node],
14321432 .elem_count = data.lhs,
1433 .sentinel = null,
1433 .sentinel = 0,
14341434 .elem_type = data.rhs,
14351435 },
14361436 };
......@@ -1440,6 +1440,7 @@ pub const Tree = struct {
14401440 assert(tree.nodes.items(.tag)[node] == .array_type_sentinel);
14411441 const data = tree.nodes.items(.data)[node];
14421442 const extra = tree.extraData(data.rhs, Node.ArrayTypeSentinel);
1443 assert(extra.sentinel != 0);
14431444 return .{
14441445 .ast = .{
14451446 .lbracket = tree.nodes.items(.main_token)[node],
......@@ -2262,7 +2263,7 @@ pub const full = struct {
22622263 pub const Ast = struct {
22632264 lbracket: TokenIndex,
22642265 elem_count: Node.Index,
2265 sentinel: ?Node.Index,
2266 sentinel: Node.Index,
22662267 elem_type: Node.Index,
22672268 };
22682269 };
......@@ -2549,9 +2550,9 @@ pub const Node = struct {
25492550 @"await",
25502551 /// `?lhs`. rhs unused. main_token is the `?`.
25512552 optional_type,
2552 /// `[lhs]rhs`. lhs can be omitted to make it a slice.
2553 /// `[lhs]rhs`.
25532554 array_type,
2554 /// `[lhs:a]b`. `array_type_sentinel[rhs]`.
2555 /// `[lhs:a]b`. `ArrayTypeSentinel[rhs]`.
25552556 array_type_sentinel,
25562557 /// `[*]align(lhs) rhs`. lhs can be omitted.
25572558 /// `*align(lhs) rhs`. lhs can be omitted.
lib/std/zig/render.zig+3-3
......@@ -717,9 +717,9 @@ fn renderArrayType(
717717 ais.pushIndentNextLine();
718718 try renderToken(ais, tree, array_type.ast.lbracket, inner_space); // lbracket
719719 try renderExpression(gpa, ais, tree, array_type.ast.elem_count, inner_space);
720 if (array_type.ast.sentinel) |sentinel| {
721 try renderToken(ais, tree, tree.firstToken(sentinel) - 1, inner_space); // colon
722 try renderExpression(gpa, ais, tree, sentinel, inner_space);
720 if (array_type.ast.sentinel != 0) {
721 try renderToken(ais, tree, tree.firstToken(array_type.ast.sentinel) - 1, inner_space); // colon
722 try renderExpression(gpa, ais, tree, array_type.ast.sentinel, inner_space);
723723 }
724724 ais.popIndent();
725725 try renderToken(ais, tree, rbracket, .none); // rbracket
src/Module.zig+26
......@@ -1210,6 +1210,32 @@ pub const Scope = struct {
12101210 return new_index + gz.zir_code.ref_start_index;
12111211 }
12121212
1213 pub fn addArrayTypeSentinel(
1214 gz: *GenZir,
1215 len: zir.Inst.Ref,
1216 sentinel: zir.Inst.Ref,
1217 elem_type: zir.Inst.Ref,
1218 ) !zir.Inst.Ref {
1219 const gpa = gz.zir_code.gpa;
1220 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
1221 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
1222
1223 const payload_index = try gz.zir_code.addExtra(zir.Inst.ArrayTypeSentinel{
1224 .sentinel = sentinel,
1225 .elem_type = elem_type,
1226 });
1227 const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
1228 gz.zir_code.instructions.appendAssumeCapacity(.{
1229 .tag = .array_type_sentinel,
1230 .data = .{ .array_type_sentinel = .{
1231 .len = len,
1232 .payload_index = payload_index,
1233 } },
1234 });
1235 gz.instructions.appendAssumeCapacity(new_index);
1236 return new_index + gz.zir_code.ref_start_index;
1237 }
1238
12131239 pub fn addUnTok(
12141240 gz: *GenZir,
12151241 tag: zir.Inst.Tag,
src/astgen.zig+13-36
......@@ -1390,56 +1390,33 @@ fn ptrType(
13901390}
13911391
13921392fn arrayType(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !zir.Inst.Ref {
1393 if (true) @panic("TODO update for zir-memory-layout");
13941393 const tree = scope.tree();
1395 const main_tokens = tree.nodes.items(.main_token);
13961394 const node_datas = tree.nodes.items(.data);
1395 const gz = scope.getGenZir();
1396 const usize_type = @enumToInt(zir.Const.usize_type);
13971397
1398 const usize_type = try addZIRInstConst(mod, scope, src, .{
1399 .ty = Type.initTag(.type),
1400 .val = Value.initTag(.usize_type),
1401 });
1402 const len_node = node_datas[node].lhs;
1403 const elem_node = node_datas[node].rhs;
1404 if (len_node == 0) {
1405 const elem_type = try typeExpr(mod, scope, elem_node);
1406 const result = try addZIRUnOp(mod, scope, src, .mut_slice_type, elem_type);
1407 return rvalue(mod, scope, rl, result);
1408 } else {
1409 // TODO check for [_]T
1410 const len = try expr(mod, scope, .{ .ty = usize_type }, len_node);
1411 const elem_type = try typeExpr(mod, scope, elem_node);
1398 // TODO check for [_]T
1399 const len = try expr(mod, scope, .{ .ty = usize_type }, node_datas[node].lhs);
1400 const elem_type = try typeExpr(mod, scope, node_datas[node].rhs);
14121401
1413 const result = try addZIRBinOp(mod, scope, src, .array_type, len, elem_type);
1414 return rvalue(mod, scope, rl, result);
1415 }
1402 const result = try gz.addBin(.array_type, len, elem_type);
1403 return rvalue(mod, scope, rl, result, node);
14161404}
14171405
14181406fn arrayTypeSentinel(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !zir.Inst.Ref {
1419 if (true) @panic("TODO update for zir-memory-layout");
14201407 const tree = scope.tree();
1421 const main_tokens = tree.nodes.items(.main_token);
14221408 const node_datas = tree.nodes.items(.data);
1423
1424 const len_node = node_datas[node].lhs;
14251409 const extra = tree.extraData(node_datas[node].rhs, ast.Node.ArrayTypeSentinel);
1426 const usize_type = try addZIRInstConst(mod, scope, src, .{
1427 .ty = Type.initTag(.type),
1428 .val = Value.initTag(.usize_type),
1429 });
1410 const gz = scope.getGenZir();
1411 const usize_type = @enumToInt(zir.Const.usize_type);
14301412
14311413 // TODO check for [_]T
1432 const len = try expr(mod, scope, .{ .ty = usize_type }, len_node);
1433 const sentinel_uncasted = try expr(mod, scope, .none, extra.sentinel);
1414 const len = try expr(mod, scope, .{ .ty = usize_type }, node_datas[node].lhs);
14341415 const elem_type = try typeExpr(mod, scope, extra.elem_type);
1435 const sentinel = try addZIRBinOp(mod, scope, src, .as, elem_type, sentinel_uncasted);
1416 const sentinel = try expr(mod, scope, .{ .ty = elem_type }, extra.sentinel);
14361417
1437 const result = try addZIRInst(mod, scope, src, zir.Inst.ArrayTypeSentinel, .{
1438 .len = len,
1439 .sentinel = sentinel,
1440 .elem_type = elem_type,
1441 }, .{});
1442 return rvalue(mod, scope, rl, result);
1418 const result = try gz.addArrayTypeSentinel(len, elem_type, sentinel);
1419 return rvalue(mod, scope, rl, result, node);
14431420}
14441421
14451422fn containerDecl(