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 {...@@ -1430,7 +1430,7 @@ pub const Tree = struct {
1430 .ast = .{1430 .ast = .{
1431 .lbracket = tree.nodes.items(.main_token)[node],1431 .lbracket = tree.nodes.items(.main_token)[node],
1432 .elem_count = data.lhs,1432 .elem_count = data.lhs,
1433 .sentinel = null,1433 .sentinel = 0,
1434 .elem_type = data.rhs,1434 .elem_type = data.rhs,
1435 },1435 },
1436 };1436 };
...@@ -1440,6 +1440,7 @@ pub const Tree = struct {...@@ -1440,6 +1440,7 @@ pub const Tree = struct {
1440 assert(tree.nodes.items(.tag)[node] == .array_type_sentinel);1440 assert(tree.nodes.items(.tag)[node] == .array_type_sentinel);
1441 const data = tree.nodes.items(.data)[node];1441 const data = tree.nodes.items(.data)[node];
1442 const extra = tree.extraData(data.rhs, Node.ArrayTypeSentinel);1442 const extra = tree.extraData(data.rhs, Node.ArrayTypeSentinel);
1443 assert(extra.sentinel != 0);
1443 return .{1444 return .{
1444 .ast = .{1445 .ast = .{
1445 .lbracket = tree.nodes.items(.main_token)[node],1446 .lbracket = tree.nodes.items(.main_token)[node],
...@@ -2262,7 +2263,7 @@ pub const full = struct {...@@ -2262,7 +2263,7 @@ pub const full = struct {
2262 pub const Ast = struct {2263 pub const Ast = struct {
2263 lbracket: TokenIndex,2264 lbracket: TokenIndex,
2264 elem_count: Node.Index,2265 elem_count: Node.Index,
2265 sentinel: ?Node.Index,2266 sentinel: Node.Index,
2266 elem_type: Node.Index,2267 elem_type: Node.Index,
2267 };2268 };
2268 };2269 };
...@@ -2549,9 +2550,9 @@ pub const Node = struct {...@@ -2549,9 +2550,9 @@ pub const Node = struct {
2549 @"await",2550 @"await",
2550 /// `?lhs`. rhs unused. main_token is the `?`.2551 /// `?lhs`. rhs unused. main_token is the `?`.
2551 optional_type,2552 optional_type,
2552 /// `[lhs]rhs`. lhs can be omitted to make it a slice.2553 /// `[lhs]rhs`.
2553 array_type,2554 array_type,
2554 /// `[lhs:a]b`. `array_type_sentinel[rhs]`.2555 /// `[lhs:a]b`. `ArrayTypeSentinel[rhs]`.
2555 array_type_sentinel,2556 array_type_sentinel,
2556 /// `[*]align(lhs) rhs`. lhs can be omitted.2557 /// `[*]align(lhs) rhs`. lhs can be omitted.
2557 /// `*align(lhs) rhs`. lhs can be omitted.2558 /// `*align(lhs) rhs`. lhs can be omitted.
lib/std/zig/render.zig+3-3
...@@ -717,9 +717,9 @@ fn renderArrayType(...@@ -717,9 +717,9 @@ fn renderArrayType(
717 ais.pushIndentNextLine();717 ais.pushIndentNextLine();
718 try renderToken(ais, tree, array_type.ast.lbracket, inner_space); // lbracket718 try renderToken(ais, tree, array_type.ast.lbracket, inner_space); // lbracket
719 try renderExpression(gpa, ais, tree, array_type.ast.elem_count, inner_space);719 try renderExpression(gpa, ais, tree, array_type.ast.elem_count, inner_space);
720 if (array_type.ast.sentinel) |sentinel| {720 if (array_type.ast.sentinel != 0) {
721 try renderToken(ais, tree, tree.firstToken(sentinel) - 1, inner_space); // colon721 try renderToken(ais, tree, tree.firstToken(array_type.ast.sentinel) - 1, inner_space); // colon
722 try renderExpression(gpa, ais, tree, sentinel, inner_space);722 try renderExpression(gpa, ais, tree, array_type.ast.sentinel, inner_space);
723 }723 }
724 ais.popIndent();724 ais.popIndent();
725 try renderToken(ais, tree, rbracket, .none); // rbracket725 try renderToken(ais, tree, rbracket, .none); // rbracket
src/Module.zig+26
...@@ -1210,6 +1210,32 @@ pub const Scope = struct {...@@ -1210,6 +1210,32 @@ pub const Scope = struct {
1210 return new_index + gz.zir_code.ref_start_index;1210 return new_index + gz.zir_code.ref_start_index;
1211 }1211 }
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
1213 pub fn addUnTok(1239 pub fn addUnTok(
1214 gz: *GenZir,1240 gz: *GenZir,
1215 tag: zir.Inst.Tag,1241 tag: zir.Inst.Tag,
src/astgen.zig+13-36
...@@ -1390,56 +1390,33 @@ fn ptrType(...@@ -1390,56 +1390,33 @@ fn ptrType(
1390}1390}
13911391
1392fn arrayType(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !zir.Inst.Ref {1392fn arrayType(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !zir.Inst.Ref {
1393 if (true) @panic("TODO update for zir-memory-layout");
1394 const tree = scope.tree();1393 const tree = scope.tree();
1395 const main_tokens = tree.nodes.items(.main_token);
1396 const node_datas = tree.nodes.items(.data);1394 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, .{1398 // TODO check for [_]T
1399 .ty = Type.initTag(.type),1399 const len = try expr(mod, scope, .{ .ty = usize_type }, node_datas[node].lhs);
1400 .val = Value.initTag(.usize_type),1400 const elem_type = try typeExpr(mod, scope, node_datas[node].rhs);
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);
14121401
1413 const result = try addZIRBinOp(mod, scope, src, .array_type, len, elem_type);1402 const result = try gz.addBin(.array_type, len, elem_type);
1414 return rvalue(mod, scope, rl, result);1403 return rvalue(mod, scope, rl, result, node);
1415 }
1416}1404}
14171405
1418fn arrayTypeSentinel(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !zir.Inst.Ref {1406fn arrayTypeSentinel(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) !zir.Inst.Ref {
1419 if (true) @panic("TODO update for zir-memory-layout");
1420 const tree = scope.tree();1407 const tree = scope.tree();
1421 const main_tokens = tree.nodes.items(.main_token);
1422 const node_datas = tree.nodes.items(.data);1408 const node_datas = tree.nodes.items(.data);
1423
1424 const len_node = node_datas[node].lhs;
1425 const extra = tree.extraData(node_datas[node].rhs, ast.Node.ArrayTypeSentinel);1409 const extra = tree.extraData(node_datas[node].rhs, ast.Node.ArrayTypeSentinel);
1426 const usize_type = try addZIRInstConst(mod, scope, src, .{1410 const gz = scope.getGenZir();
1427 .ty = Type.initTag(.type),1411 const usize_type = @enumToInt(zir.Const.usize_type);
1428 .val = Value.initTag(.usize_type),
1429 });
14301412
1431 // TODO check for [_]T1413 // TODO check for [_]T
1432 const len = try expr(mod, scope, .{ .ty = usize_type }, len_node);1414 const len = try expr(mod, scope, .{ .ty = usize_type }, node_datas[node].lhs);
1433 const sentinel_uncasted = try expr(mod, scope, .none, extra.sentinel);
1434 const elem_type = try typeExpr(mod, scope, extra.elem_type);1415 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, .{1418 const result = try gz.addArrayTypeSentinel(len, elem_type, sentinel);
1438 .len = len,1419 return rvalue(mod, scope, rl, result, node);
1439 .sentinel = sentinel,
1440 .elem_type = elem_type,
1441 }, .{});
1442 return rvalue(mod, scope, rl, result);
1443}1420}
14441421
1445fn containerDecl(1422fn containerDecl(