| ... | ... | @@ -1574,8 +1574,7 @@ fn varDecl( |
| 1574 | 1574 | // const local, and type inference becomes trivial. |
| 1575 | 1575 | // Move the init_scope instructions into the parent scope, eliding |
| 1576 | 1576 | // the alloc instruction and the store_to_block_ptr instruction. |
| 1577 | | const expected_len = parent_zir.items.len + init_scope.instructions.items.len - 2; |
| 1578 | | try parent_zir.ensureCapacity(gpa, expected_len); |
| 1577 | try parent_zir.ensureUnusedCapacity(gpa, init_scope.instructions.items.len); |
| 1579 | 1578 | for (init_scope.instructions.items) |src_inst| { |
| 1580 | 1579 | if (gz.indexToRef(src_inst) == init_scope.rl_ptr) continue; |
| 1581 | 1580 | if (zir_tags[src_inst] == .store_to_block_ptr) { |
| ... | ... | @@ -1583,7 +1582,6 @@ fn varDecl( |
| 1583 | 1582 | } |
| 1584 | 1583 | parent_zir.appendAssumeCapacity(src_inst); |
| 1585 | 1584 | } |
| 1586 | | assert(parent_zir.items.len == expected_len); |
| 1587 | 1585 | |
| 1588 | 1586 | const sub_scope = try block_arena.create(Scope.LocalVal); |
| 1589 | 1587 | sub_scope.* = .{ |
| ... | ... | @@ -1907,6 +1905,15 @@ fn fnDecl( |
| 1907 | 1905 | const param_types = try gpa.alloc(Zir.Inst.Ref, param_count); |
| 1908 | 1906 | defer gpa.free(param_types); |
| 1909 | 1907 | |
| 1908 | var decl_gz: Scope.GenZir = .{ |
| 1909 | .force_comptime = true, |
| 1910 | .decl_node_index = fn_proto.ast.proto_node, |
| 1911 | .parent = &gz.base, |
| 1912 | .astgen = astgen, |
| 1913 | .ref_start_index = @intCast(u32, Zir.Inst.Ref.typed_value_map.len + param_count), |
| 1914 | }; |
| 1915 | defer decl_gz.instructions.deinit(gpa); |
| 1916 | |
| 1910 | 1917 | var is_var_args = false; |
| 1911 | 1918 | { |
| 1912 | 1919 | var param_type_i: usize = 0; |
| ... | ... | @@ -1929,13 +1936,13 @@ fn fnDecl( |
| 1929 | 1936 | const param_type_node = param.type_expr; |
| 1930 | 1937 | assert(param_type_node != 0); |
| 1931 | 1938 | param_types[param_type_i] = |
| 1932 | | try expr(gz, &gz.base, .{ .ty = .type_type }, param_type_node); |
| 1939 | try expr(&decl_gz, &decl_gz.base, .{ .ty = .type_type }, param_type_node); |
| 1933 | 1940 | } |
| 1934 | 1941 | assert(param_type_i == param_count); |
| 1935 | 1942 | } |
| 1936 | 1943 | |
| 1937 | 1944 | const lib_name: u32 = if (fn_proto.lib_name) |lib_name_token| blk: { |
| 1938 | | const lib_name_str = try gz.strLitAsString(lib_name_token); |
| 1945 | const lib_name_str = try decl_gz.strLitAsString(lib_name_token); |
| 1939 | 1946 | break :blk lib_name_str.index; |
| 1940 | 1947 | } else 0; |
| 1941 | 1948 | |
| ... | ... | @@ -1959,8 +1966,8 @@ fn fnDecl( |
| 1959 | 1966 | return astgen.failTok(maybe_bang, "TODO implement inferred error sets", .{}); |
| 1960 | 1967 | } |
| 1961 | 1968 | const return_type_inst = try AstGen.expr( |
| 1962 | | gz, |
| 1963 | | &gz.base, |
| 1969 | &decl_gz, |
| 1970 | &decl_gz.base, |
| 1964 | 1971 | .{ .ty = .type_type }, |
| 1965 | 1972 | fn_proto.ast.return_type, |
| 1966 | 1973 | ); |
| ... | ... | @@ -1969,14 +1976,14 @@ fn fnDecl( |
| 1969 | 1976 | // TODO instead of enum literal type, this needs to be the |
| 1970 | 1977 | // std.builtin.CallingConvention enum. We need to implement importing other files |
| 1971 | 1978 | // and enums in order to fix this. |
| 1972 | | try AstGen.comptimeExpr( |
| 1973 | | gz, |
| 1974 | | &gz.base, |
| 1979 | try AstGen.expr( |
| 1980 | &decl_gz, |
| 1981 | &decl_gz.base, |
| 1975 | 1982 | .{ .ty = .enum_literal_type }, |
| 1976 | 1983 | fn_proto.ast.callconv_expr, |
| 1977 | 1984 | ) |
| 1978 | 1985 | else if (is_extern) // note: https://github.com/ziglang/zig/issues/5269 |
| 1979 | | try gz.addSmallStr(.enum_literal_small, "C") |
| 1986 | try decl_gz.addSmallStr(.enum_literal_small, "C") |
| 1980 | 1987 | else |
| 1981 | 1988 | .none; |
| 1982 | 1989 | |
| ... | ... | @@ -1987,7 +1994,7 @@ fn fnDecl( |
| 1987 | 1994 | |
| 1988 | 1995 | if (cc != .none or lib_name != 0) { |
| 1989 | 1996 | const tag: Zir.Inst.Tag = if (is_var_args) .func_extra_var_args else .func_extra; |
| 1990 | | break :func try gz.addFuncExtra(tag, .{ |
| 1997 | break :func try decl_gz.addFuncExtra(tag, .{ |
| 1991 | 1998 | .src_node = fn_proto.ast.proto_node, |
| 1992 | 1999 | .ret_ty = return_type_inst, |
| 1993 | 2000 | .param_types = param_types, |
| ... | ... | @@ -1998,7 +2005,7 @@ fn fnDecl( |
| 1998 | 2005 | } |
| 1999 | 2006 | |
| 2000 | 2007 | const tag: Zir.Inst.Tag = if (is_var_args) .func_var_args else .func; |
| 2001 | | break :func try gz.addFunc(tag, .{ |
| 2008 | break :func try decl_gz.addFunc(tag, .{ |
| 2002 | 2009 | .src_node = fn_proto.ast.proto_node, |
| 2003 | 2010 | .ret_ty = return_type_inst, |
| 2004 | 2011 | .param_types = param_types, |
| ... | ... | @@ -2012,7 +2019,7 @@ fn fnDecl( |
| 2012 | 2019 | var fn_gz: Scope.GenZir = .{ |
| 2013 | 2020 | .force_comptime = false, |
| 2014 | 2021 | .decl_node_index = fn_proto.ast.proto_node, |
| 2015 | | .parent = &gz.base, |
| 2022 | .parent = &decl_gz.base, |
| 2016 | 2023 | .astgen = astgen, |
| 2017 | 2024 | .ref_start_index = @intCast(u32, Zir.Inst.Ref.typed_value_map.len + param_count), |
| 2018 | 2025 | }; |
| ... | ... | @@ -2061,7 +2068,7 @@ fn fnDecl( |
| 2061 | 2068 | |
| 2062 | 2069 | if (cc != .none or lib_name != 0) { |
| 2063 | 2070 | const tag: Zir.Inst.Tag = if (is_var_args) .func_extra_var_args else .func_extra; |
| 2064 | | break :func try fn_gz.addFuncExtra(tag, .{ |
| 2071 | break :func try decl_gz.addFuncExtra(tag, .{ |
| 2065 | 2072 | .src_node = fn_proto.ast.proto_node, |
| 2066 | 2073 | .ret_ty = return_type_inst, |
| 2067 | 2074 | .param_types = param_types, |
| ... | ... | @@ -2072,7 +2079,7 @@ fn fnDecl( |
| 2072 | 2079 | } |
| 2073 | 2080 | |
| 2074 | 2081 | const tag: Zir.Inst.Tag = if (is_var_args) .func_var_args else .func; |
| 2075 | | break :func try fn_gz.addFunc(tag, .{ |
| 2082 | break :func try decl_gz.addFunc(tag, .{ |
| 2076 | 2083 | .src_node = fn_proto.ast.proto_node, |
| 2077 | 2084 | .ret_ty = return_type_inst, |
| 2078 | 2085 | .param_types = param_types, |
| ... | ... | @@ -2083,11 +2090,15 @@ fn fnDecl( |
| 2083 | 2090 | const fn_name_token = fn_proto.name_token orelse { |
| 2084 | 2091 | return astgen.failTok(fn_proto.ast.fn_token, "missing function name", .{}); |
| 2085 | 2092 | }; |
| 2086 | | const fn_name_str_index = try gz.identAsString(fn_name_token); |
| 2093 | const fn_name_str_index = try decl_gz.identAsString(fn_name_token); |
| 2094 | |
| 2095 | const block_inst = try gz.addBlock(.block_inline, fn_proto.ast.proto_node); |
| 2096 | _ = try decl_gz.addBreak(.break_inline, block_inst, func_inst); |
| 2097 | try decl_gz.setBlockBody(block_inst); |
| 2087 | 2098 | |
| 2088 | 2099 | try wip_decls.name_and_value.ensureCapacity(gpa, wip_decls.name_and_value.items.len + 2); |
| 2089 | 2100 | wip_decls.name_and_value.appendAssumeCapacity(fn_name_str_index); |
| 2090 | | wip_decls.name_and_value.appendAssumeCapacity(@enumToInt(func_inst)); |
| 2101 | wip_decls.name_and_value.appendAssumeCapacity(block_inst); |
| 2091 | 2102 | } |
| 2092 | 2103 | |
| 2093 | 2104 | fn globalVarDecl( |
| ... | ... | @@ -2175,8 +2186,6 @@ fn globalVarDecl( |
| 2175 | 2186 | ); |
| 2176 | 2187 | |
| 2177 | 2188 | const tag: Zir.Inst.Tag = if (is_mutable) .block_inline_var else .block_inline; |
| 2178 | | // const globals are just their instruction. mutable globals have |
| 2179 | | // a special ZIR form. |
| 2180 | 2189 | const block_inst = try gz.addBlock(tag, node); |
| 2181 | 2190 | _ = try block_scope.addBreak(.break_inline, block_inst, init_inst); |
| 2182 | 2191 | try block_scope.setBlockBody(block_inst); |