authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-16 19:45:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-16 19:45:24-07:00
log415ef1be510e912ddbd3b73a4d20aac3fda27a0e
tree0fcc2e2c008e74bd9c2a2e6cc18507a48200fb8d
parentdd5a1b1106b3aed3aacd54e9615d96703d29ed9d

AstGen: fix function decl astgen

it was using the wrong scope and other mistakes too

1 files changed, 29 insertions(+), 20 deletions(-)

src/AstGen.zig+29-20
...@@ -1574,8 +1574,7 @@ fn varDecl(...@@ -1574,8 +1574,7 @@ fn varDecl(
1574 // const local, and type inference becomes trivial.1574 // const local, and type inference becomes trivial.
1575 // Move the init_scope instructions into the parent scope, eliding1575 // Move the init_scope instructions into the parent scope, eliding
1576 // the alloc instruction and the store_to_block_ptr instruction.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;1577 try parent_zir.ensureUnusedCapacity(gpa, init_scope.instructions.items.len);
1578 try parent_zir.ensureCapacity(gpa, expected_len);
1579 for (init_scope.instructions.items) |src_inst| {1578 for (init_scope.instructions.items) |src_inst| {
1580 if (gz.indexToRef(src_inst) == init_scope.rl_ptr) continue;1579 if (gz.indexToRef(src_inst) == init_scope.rl_ptr) continue;
1581 if (zir_tags[src_inst] == .store_to_block_ptr) {1580 if (zir_tags[src_inst] == .store_to_block_ptr) {
...@@ -1583,7 +1582,6 @@ fn varDecl(...@@ -1583,7 +1582,6 @@ fn varDecl(
1583 }1582 }
1584 parent_zir.appendAssumeCapacity(src_inst);1583 parent_zir.appendAssumeCapacity(src_inst);
1585 }1584 }
1586 assert(parent_zir.items.len == expected_len);
15871585
1588 const sub_scope = try block_arena.create(Scope.LocalVal);1586 const sub_scope = try block_arena.create(Scope.LocalVal);
1589 sub_scope.* = .{1587 sub_scope.* = .{
...@@ -1907,6 +1905,15 @@ fn fnDecl(...@@ -1907,6 +1905,15 @@ fn fnDecl(
1907 const param_types = try gpa.alloc(Zir.Inst.Ref, param_count);1905 const param_types = try gpa.alloc(Zir.Inst.Ref, param_count);
1908 defer gpa.free(param_types);1906 defer gpa.free(param_types);
19091907
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 var is_var_args = false;1917 var is_var_args = false;
1911 {1918 {
1912 var param_type_i: usize = 0;1919 var param_type_i: usize = 0;
...@@ -1929,13 +1936,13 @@ fn fnDecl(...@@ -1929,13 +1936,13 @@ fn fnDecl(
1929 const param_type_node = param.type_expr;1936 const param_type_node = param.type_expr;
1930 assert(param_type_node != 0);1937 assert(param_type_node != 0);
1931 param_types[param_type_i] =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 assert(param_type_i == param_count);1941 assert(param_type_i == param_count);
1935 }1942 }
19361943
1937 const lib_name: u32 = if (fn_proto.lib_name) |lib_name_token| blk: {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 break :blk lib_name_str.index;1946 break :blk lib_name_str.index;
1940 } else 0;1947 } else 0;
19411948
...@@ -1959,8 +1966,8 @@ fn fnDecl(...@@ -1959,8 +1966,8 @@ fn fnDecl(
1959 return astgen.failTok(maybe_bang, "TODO implement inferred error sets", .{});1966 return astgen.failTok(maybe_bang, "TODO implement inferred error sets", .{});
1960 }1967 }
1961 const return_type_inst = try AstGen.expr(1968 const return_type_inst = try AstGen.expr(
1962 gz,1969 &decl_gz,
1963 &gz.base,1970 &decl_gz.base,
1964 .{ .ty = .type_type },1971 .{ .ty = .type_type },
1965 fn_proto.ast.return_type,1972 fn_proto.ast.return_type,
1966 );1973 );
...@@ -1969,14 +1976,14 @@ fn fnDecl(...@@ -1969,14 +1976,14 @@ fn fnDecl(
1969 // TODO instead of enum literal type, this needs to be the1976 // TODO instead of enum literal type, this needs to be the
1970 // std.builtin.CallingConvention enum. We need to implement importing other files1977 // std.builtin.CallingConvention enum. We need to implement importing other files
1971 // and enums in order to fix this.1978 // and enums in order to fix this.
1972 try AstGen.comptimeExpr(1979 try AstGen.expr(
1973 gz,1980 &decl_gz,
1974 &gz.base,1981 &decl_gz.base,
1975 .{ .ty = .enum_literal_type },1982 .{ .ty = .enum_literal_type },
1976 fn_proto.ast.callconv_expr,1983 fn_proto.ast.callconv_expr,
1977 )1984 )
1978 else if (is_extern) // note: https://github.com/ziglang/zig/issues/52691985 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 else1987 else
1981 .none;1988 .none;
19821989
...@@ -1987,7 +1994,7 @@ fn fnDecl(...@@ -1987,7 +1994,7 @@ fn fnDecl(
19871994
1988 if (cc != .none or lib_name != 0) {1995 if (cc != .none or lib_name != 0) {
1989 const tag: Zir.Inst.Tag = if (is_var_args) .func_extra_var_args else .func_extra;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 .src_node = fn_proto.ast.proto_node,1998 .src_node = fn_proto.ast.proto_node,
1992 .ret_ty = return_type_inst,1999 .ret_ty = return_type_inst,
1993 .param_types = param_types,2000 .param_types = param_types,
...@@ -1998,7 +2005,7 @@ fn fnDecl(...@@ -1998,7 +2005,7 @@ fn fnDecl(
1998 }2005 }
19992006
2000 const tag: Zir.Inst.Tag = if (is_var_args) .func_var_args else .func;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 .src_node = fn_proto.ast.proto_node,2009 .src_node = fn_proto.ast.proto_node,
2003 .ret_ty = return_type_inst,2010 .ret_ty = return_type_inst,
2004 .param_types = param_types,2011 .param_types = param_types,
...@@ -2012,7 +2019,7 @@ fn fnDecl(...@@ -2012,7 +2019,7 @@ fn fnDecl(
2012 var fn_gz: Scope.GenZir = .{2019 var fn_gz: Scope.GenZir = .{
2013 .force_comptime = false,2020 .force_comptime = false,
2014 .decl_node_index = fn_proto.ast.proto_node,2021 .decl_node_index = fn_proto.ast.proto_node,
2015 .parent = &gz.base,2022 .parent = &decl_gz.base,
2016 .astgen = astgen,2023 .astgen = astgen,
2017 .ref_start_index = @intCast(u32, Zir.Inst.Ref.typed_value_map.len + param_count),2024 .ref_start_index = @intCast(u32, Zir.Inst.Ref.typed_value_map.len + param_count),
2018 };2025 };
...@@ -2061,7 +2068,7 @@ fn fnDecl(...@@ -2061,7 +2068,7 @@ fn fnDecl(
20612068
2062 if (cc != .none or lib_name != 0) {2069 if (cc != .none or lib_name != 0) {
2063 const tag: Zir.Inst.Tag = if (is_var_args) .func_extra_var_args else .func_extra;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 .src_node = fn_proto.ast.proto_node,2072 .src_node = fn_proto.ast.proto_node,
2066 .ret_ty = return_type_inst,2073 .ret_ty = return_type_inst,
2067 .param_types = param_types,2074 .param_types = param_types,
...@@ -2072,7 +2079,7 @@ fn fnDecl(...@@ -2072,7 +2079,7 @@ fn fnDecl(
2072 }2079 }
20732080
2074 const tag: Zir.Inst.Tag = if (is_var_args) .func_var_args else .func;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 .src_node = fn_proto.ast.proto_node,2083 .src_node = fn_proto.ast.proto_node,
2077 .ret_ty = return_type_inst,2084 .ret_ty = return_type_inst,
2078 .param_types = param_types,2085 .param_types = param_types,
...@@ -2083,11 +2090,15 @@ fn fnDecl(...@@ -2083,11 +2090,15 @@ fn fnDecl(
2083 const fn_name_token = fn_proto.name_token orelse {2090 const fn_name_token = fn_proto.name_token orelse {
2084 return astgen.failTok(fn_proto.ast.fn_token, "missing function name", .{});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);
20872098
2088 try wip_decls.name_and_value.ensureCapacity(gpa, wip_decls.name_and_value.items.len + 2);2099 try wip_decls.name_and_value.ensureCapacity(gpa, wip_decls.name_and_value.items.len + 2);
2089 wip_decls.name_and_value.appendAssumeCapacity(fn_name_str_index);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}
20922103
2093fn globalVarDecl(2104fn globalVarDecl(
...@@ -2175,8 +2186,6 @@ fn globalVarDecl(...@@ -2175,8 +2186,6 @@ fn globalVarDecl(
2175 );2186 );
21762187
2177 const tag: Zir.Inst.Tag = if (is_mutable) .block_inline_var else .block_inline;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 const block_inst = try gz.addBlock(tag, node);2189 const block_inst = try gz.addBlock(tag, node);
2181 _ = try block_scope.addBreak(.break_inline, block_inst, init_inst);2190 _ = try block_scope.addBreak(.break_inline, block_inst, init_inst);
2182 try block_scope.setBlockBody(block_inst);2191 try block_scope.setBlockBody(block_inst);