authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-29 19:56:01-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-29 19:56:01-07:00
log8944240aec6b53106856bb0ac2eb9da180f6b326
tree47672df25bcc997cee0aa61ebe28e0709704c980
parent86d564eed8b9eacd0447598dd364ab04c5b1b04d

AstGen: represent global variables directly

Rather than with `block_inline_var`. This matches how function declarations work and how extern variables work.

4 files changed, 30 insertions(+), 25 deletions(-)

src/AstGen.zig+27-17
......@@ -1854,7 +1854,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner
18541854 .bit_or,
18551855 .block,
18561856 .block_inline,
1857 .block_inline_var,
18581857 .suspend_block,
18591858 .loop,
18601859 .bool_br_and,
......@@ -2917,10 +2916,9 @@ fn globalVarDecl(
29172916 const token_tags = tree.tokens.items(.tag);
29182917
29192918 const is_mutable = token_tags[var_decl.ast.mut_token] == .keyword_var;
2920 const tag: Zir.Inst.Tag = if (is_mutable) .block_inline_var else .block_inline;
29212919 // We do this at the beginning so that the instruction index marks the range start
29222920 // of the top level declaration.
2923 const block_inst = try gz.addBlock(tag, node);
2921 const block_inst = try gz.addBlock(.block_inline, node);
29242922
29252923 var block_scope: GenZir = .{
29262924 .parent = scope,
......@@ -2961,7 +2959,7 @@ fn globalVarDecl(
29612959
29622960 assert(var_decl.comptime_token == null); // handled by parser
29632961
2964 if (var_decl.ast.init_node != 0) {
2962 const var_inst: Zir.Inst.Ref = if (var_decl.ast.init_node != 0) vi: {
29652963 if (is_extern) {
29662964 return astgen.failNode(
29672965 var_decl.ast.init_node,
......@@ -2970,43 +2968,55 @@ fn globalVarDecl(
29702968 );
29712969 }
29722970
2973 const init_result_loc: AstGen.ResultLoc = if (var_decl.ast.type_node != 0) .{
2974 .ty = try expr(
2971 const type_inst: Zir.Inst.Ref = if (var_decl.ast.type_node != 0)
2972 try expr(
29752973 &block_scope,
29762974 &block_scope.base,
29772975 .{ .ty = .type_type },
29782976 var_decl.ast.type_node,
2979 ),
2980 } else .none;
2977 )
2978 else
2979 .none;
29812980
29822981 const init_inst = try expr(
29832982 &block_scope,
29842983 &block_scope.base,
2985 init_result_loc,
2984 if (type_inst != .none) .{ .ty = type_inst } else .none,
29862985 var_decl.ast.init_node,
29872986 );
29882987
2989 // We do this at the end so that the instruction index marks the end
2990 // range of a top level declaration.
2991 _ = try block_scope.addBreak(.break_inline, block_inst, init_inst);
2988 if (is_mutable) {
2989 const var_inst = try block_scope.addVar(.{
2990 .var_type = type_inst,
2991 .lib_name = 0,
2992 .align_inst = .none, // passed via the decls data
2993 .init = init_inst,
2994 .is_extern = false,
2995 });
2996 break :vi var_inst;
2997 } else {
2998 break :vi init_inst;
2999 }
29923000 } else if (!is_extern) {
29933001 return astgen.failNode(node, "variables must be initialized", .{});
2994 } else if (var_decl.ast.type_node != 0) {
3002 } else if (var_decl.ast.type_node != 0) vi: {
29953003 // Extern variable which has an explicit type.
29963004 const type_inst = try typeExpr(&block_scope, &block_scope.base, var_decl.ast.type_node);
29973005
29983006 const var_inst = try block_scope.addVar(.{
29993007 .var_type = type_inst,
30003008 .lib_name = lib_name,
3001 .align_inst = .none, // passed in the decls data
3009 .align_inst = .none, // passed via the decls data
30023010 .init = .none,
30033011 .is_extern = true,
30043012 });
3005
3006 _ = try block_scope.addBreak(.break_inline, block_inst, var_inst);
3013 break :vi var_inst;
30073014 } else {
30083015 return astgen.failNode(node, "unable to infer variable type", .{});
3009 }
3016 };
3017 // We do this at the end so that the instruction index marks the end
3018 // range of a top level declaration.
3019 _ = try block_scope.addBreak(.break_inline, block_inst, var_inst);
30103020 try block_scope.setBlockBody(block_inst);
30113021
30123022 const name_token = var_decl.ast.mut_token + 1;
src/Module.zig+1-1
......@@ -3478,7 +3478,7 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
34783478 }
34793479 return type_changed or is_inline != prev_is_inline;
34803480 } else {
3481 const is_mutable = zir_tags[zir_block_index] == .block_inline_var;
3481 const is_mutable = decl_tv.val.tag() == .variable;
34823482
34833483 var is_threadlocal = false; // TODO implement threadlocal variables
34843484 var is_extern = false; // TODO implement extern variables
src/Sema.zig+1-1
......@@ -439,7 +439,7 @@ pub fn analyzeBody(
439439 i = 0;
440440 continue;
441441 },
442 .block_inline, .block_inline_var => blk: {
442 .block_inline => blk: {
443443 // Directly analyze the block body without introducing a new block.
444444 const inst_data = datas[inst].pl_node;
445445 const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index);
src/Zir.zig+1-6
......@@ -214,8 +214,6 @@ pub const Inst = struct {
214214 /// a noreturn instruction.
215215 /// Uses the `pl_node` union field. Payload is `Block`.
216216 block_inline,
217 /// Same as `block_inline` but it additionally marks a decl as being a variable.
218 block_inline_var,
219217 /// Implements `suspend {...}`.
220218 /// Uses the `pl_node` union field. Payload is `Block`.
221219 suspend_block,
......@@ -982,7 +980,6 @@ pub const Inst = struct {
982980 .bit_or,
983981 .block,
984982 .block_inline,
985 .block_inline_var,
986983 .suspend_block,
987984 .loop,
988985 .bool_br_and,
......@@ -1240,7 +1237,6 @@ pub const Inst = struct {
12401237 .bit_or = .pl_node,
12411238 .block = .pl_node,
12421239 .block_inline = .pl_node,
1243 .block_inline_var = .pl_node,
12441240 .suspend_block = .pl_node,
12451241 .bool_and = .pl_node,
12461242 .bool_not = .un_node,
......@@ -2517,7 +2513,7 @@ pub const Inst = struct {
25172513 /// - if name == 0 `is_exported` determines which one: 0=comptime,1=usingnamespace
25182514 /// - 1 means test decl with no name.
25192515 /// value: Index,
2520 /// - one of: block_inline, block_inline_var
2516 /// - one of: block_inline
25212517 /// align: Ref, // if corresponding bit is set
25222518 /// link_section: Ref, // if corresponding bit is set
25232519 /// }
......@@ -2933,7 +2929,6 @@ const Writer = struct {
29332929
29342930 .block,
29352931 .block_inline,
2936 .block_inline_var,
29372932 .suspend_block,
29382933 .loop,
29392934 .validate_struct_init_ptr,