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(
15741574 // const local, and type inference becomes trivial.
15751575 // Move the init_scope instructions into the parent scope, eliding
15761576 // 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);
15791578 for (init_scope.instructions.items) |src_inst| {
15801579 if (gz.indexToRef(src_inst) == init_scope.rl_ptr) continue;
15811580 if (zir_tags[src_inst] == .store_to_block_ptr) {
......@@ -1583,7 +1582,6 @@ fn varDecl(
15831582 }
15841583 parent_zir.appendAssumeCapacity(src_inst);
15851584 }
1586 assert(parent_zir.items.len == expected_len);
15871585
15881586 const sub_scope = try block_arena.create(Scope.LocalVal);
15891587 sub_scope.* = .{
......@@ -1907,6 +1905,15 @@ fn fnDecl(
19071905 const param_types = try gpa.alloc(Zir.Inst.Ref, param_count);
19081906 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
19101917 var is_var_args = false;
19111918 {
19121919 var param_type_i: usize = 0;
......@@ -1929,13 +1936,13 @@ fn fnDecl(
19291936 const param_type_node = param.type_expr;
19301937 assert(param_type_node != 0);
19311938 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);
19331940 }
19341941 assert(param_type_i == param_count);
19351942 }
19361943
19371944 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);
19391946 break :blk lib_name_str.index;
19401947 } else 0;
19411948
......@@ -1959,8 +1966,8 @@ fn fnDecl(
19591966 return astgen.failTok(maybe_bang, "TODO implement inferred error sets", .{});
19601967 }
19611968 const return_type_inst = try AstGen.expr(
1962 gz,
1963 &gz.base,
1969 &decl_gz,
1970 &decl_gz.base,
19641971 .{ .ty = .type_type },
19651972 fn_proto.ast.return_type,
19661973 );
......@@ -1969,14 +1976,14 @@ fn fnDecl(
19691976 // TODO instead of enum literal type, this needs to be the
19701977 // std.builtin.CallingConvention enum. We need to implement importing other files
19711978 // 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,
19751982 .{ .ty = .enum_literal_type },
19761983 fn_proto.ast.callconv_expr,
19771984 )
19781985 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")
19801987 else
19811988 .none;
19821989
......@@ -1987,7 +1994,7 @@ fn fnDecl(
19871994
19881995 if (cc != .none or lib_name != 0) {
19891996 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, .{
19911998 .src_node = fn_proto.ast.proto_node,
19921999 .ret_ty = return_type_inst,
19932000 .param_types = param_types,
......@@ -1998,7 +2005,7 @@ fn fnDecl(
19982005 }
19992006
20002007 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, .{
20022009 .src_node = fn_proto.ast.proto_node,
20032010 .ret_ty = return_type_inst,
20042011 .param_types = param_types,
......@@ -2012,7 +2019,7 @@ fn fnDecl(
20122019 var fn_gz: Scope.GenZir = .{
20132020 .force_comptime = false,
20142021 .decl_node_index = fn_proto.ast.proto_node,
2015 .parent = &gz.base,
2022 .parent = &decl_gz.base,
20162023 .astgen = astgen,
20172024 .ref_start_index = @intCast(u32, Zir.Inst.Ref.typed_value_map.len + param_count),
20182025 };
......@@ -2061,7 +2068,7 @@ fn fnDecl(
20612068
20622069 if (cc != .none or lib_name != 0) {
20632070 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, .{
20652072 .src_node = fn_proto.ast.proto_node,
20662073 .ret_ty = return_type_inst,
20672074 .param_types = param_types,
......@@ -2072,7 +2079,7 @@ fn fnDecl(
20722079 }
20732080
20742081 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, .{
20762083 .src_node = fn_proto.ast.proto_node,
20772084 .ret_ty = return_type_inst,
20782085 .param_types = param_types,
......@@ -2083,11 +2090,15 @@ fn fnDecl(
20832090 const fn_name_token = fn_proto.name_token orelse {
20842091 return astgen.failTok(fn_proto.ast.fn_token, "missing function name", .{});
20852092 };
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
20882099 try wip_decls.name_and_value.ensureCapacity(gpa, wip_decls.name_and_value.items.len + 2);
20892100 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);
20912102}
20922103
20932104fn globalVarDecl(
......@@ -2175,8 +2186,6 @@ fn globalVarDecl(
21752186 );
21762187
21772188 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.
21802189 const block_inst = try gz.addBlock(tag, node);
21812190 _ = try block_scope.addBreak(.break_inline, block_inst, init_inst);
21822191 try block_scope.setBlockBody(block_inst);