| ... | @@ -1854,7 +1854,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner | ... | @@ -1854,7 +1854,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner |
| 1854 | .bit_or, | 1854 | .bit_or, |
| 1855 | .block, | 1855 | .block, |
| 1856 | .block_inline, | 1856 | .block_inline, |
| 1857 | .block_inline_var, | | |
| 1858 | .suspend_block, | 1857 | .suspend_block, |
| 1859 | .loop, | 1858 | .loop, |
| 1860 | .bool_br_and, | 1859 | .bool_br_and, |
| ... | @@ -2917,10 +2916,9 @@ fn globalVarDecl( | ... | @@ -2917,10 +2916,9 @@ fn globalVarDecl( |
| 2917 | const token_tags = tree.tokens.items(.tag); | 2916 | const token_tags = tree.tokens.items(.tag); |
| 2918 | | 2917 | |
| 2919 | const is_mutable = token_tags[var_decl.ast.mut_token] == .keyword_var; | 2918 | 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; | | |
| 2921 | // We do this at the beginning so that the instruction index marks the range start | 2919 | // We do this at the beginning so that the instruction index marks the range start |
| 2922 | // of the top level declaration. | 2920 | // of the top level declaration. |
| 2923 | const block_inst = try gz.addBlock(tag, node); | 2921 | const block_inst = try gz.addBlock(.block_inline, node); |
| 2924 | | 2922 | |
| 2925 | var block_scope: GenZir = .{ | 2923 | var block_scope: GenZir = .{ |
| 2926 | .parent = scope, | 2924 | .parent = scope, |
| ... | @@ -2961,7 +2959,7 @@ fn globalVarDecl( | ... | @@ -2961,7 +2959,7 @@ fn globalVarDecl( |
| 2961 | | 2959 | |
| 2962 | assert(var_decl.comptime_token == null); // handled by parser | 2960 | assert(var_decl.comptime_token == null); // handled by parser |
| 2963 | | 2961 | |
| 2964 | if (var_decl.ast.init_node != 0) { | 2962 | const var_inst: Zir.Inst.Ref = if (var_decl.ast.init_node != 0) vi: { |
| 2965 | if (is_extern) { | 2963 | if (is_extern) { |
| 2966 | return astgen.failNode( | 2964 | return astgen.failNode( |
| 2967 | var_decl.ast.init_node, | 2965 | var_decl.ast.init_node, |
| ... | @@ -2970,43 +2968,55 @@ fn globalVarDecl( | ... | @@ -2970,43 +2968,55 @@ fn globalVarDecl( |
| 2970 | ); | 2968 | ); |
| 2971 | } | 2969 | } |
| 2972 | | 2970 | |
| 2973 | const init_result_loc: AstGen.ResultLoc = if (var_decl.ast.type_node != 0) .{ | 2971 | const type_inst: Zir.Inst.Ref = if (var_decl.ast.type_node != 0) |
| 2974 | .ty = try expr( | 2972 | try expr( |
| 2975 | &block_scope, | 2973 | &block_scope, |
| 2976 | &block_scope.base, | 2974 | &block_scope.base, |
| 2977 | .{ .ty = .type_type }, | 2975 | .{ .ty = .type_type }, |
| 2978 | var_decl.ast.type_node, | 2976 | var_decl.ast.type_node, |
| 2979 | ), | 2977 | ) |
| 2980 | } else .none; | 2978 | else |
| | 2979 | .none; |
| 2981 | | 2980 | |
| 2982 | const init_inst = try expr( | 2981 | const init_inst = try expr( |
| 2983 | &block_scope, | 2982 | &block_scope, |
| 2984 | &block_scope.base, | 2983 | &block_scope.base, |
| 2985 | init_result_loc, | 2984 | if (type_inst != .none) .{ .ty = type_inst } else .none, |
| 2986 | var_decl.ast.init_node, | 2985 | var_decl.ast.init_node, |
| 2987 | ); | 2986 | ); |
| 2988 | | 2987 | |
| 2989 | // We do this at the end so that the instruction index marks the end | 2988 | if (is_mutable) { |
| 2990 | // range of a top level declaration. | 2989 | const var_inst = try block_scope.addVar(.{ |
| 2991 | _ = try block_scope.addBreak(.break_inline, block_inst, init_inst); | 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 | } |
| 2992 | } else if (!is_extern) { | 3000 | } else if (!is_extern) { |
| 2993 | return astgen.failNode(node, "variables must be initialized", .{}); | 3001 | 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: { |
| 2995 | // Extern variable which has an explicit type. | 3003 | // Extern variable which has an explicit type. |
| 2996 | const type_inst = try typeExpr(&block_scope, &block_scope.base, var_decl.ast.type_node); | 3004 | const type_inst = try typeExpr(&block_scope, &block_scope.base, var_decl.ast.type_node); |
| 2997 | | 3005 | |
| 2998 | const var_inst = try block_scope.addVar(.{ | 3006 | const var_inst = try block_scope.addVar(.{ |
| 2999 | .var_type = type_inst, | 3007 | .var_type = type_inst, |
| 3000 | .lib_name = lib_name, | 3008 | .lib_name = lib_name, |
| 3001 | .align_inst = .none, // passed in the decls data | 3009 | .align_inst = .none, // passed via the decls data |
| 3002 | .init = .none, | 3010 | .init = .none, |
| 3003 | .is_extern = true, | 3011 | .is_extern = true, |
| 3004 | }); | 3012 | }); |
| 3005 | | 3013 | break :vi var_inst; |
| 3006 | _ = try block_scope.addBreak(.break_inline, block_inst, var_inst); | | |
| 3007 | } else { | 3014 | } else { |
| 3008 | return astgen.failNode(node, "unable to infer variable type", .{}); | 3015 | 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); |
| 3010 | try block_scope.setBlockBody(block_inst); | 3020 | try block_scope.setBlockBody(block_inst); |
| 3011 | | 3021 | |
| 3012 | const name_token = var_decl.ast.mut_token + 1; | 3022 | const name_token = var_decl.ast.mut_token + 1; |