authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-12-17 18:50:38+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-12-21 01:41:50+01:00
logd5621504b0e2160fa44991fc4180165584d72819
tree0982bfb7e952a6a9b87c063a5431002c6d8345e1
parente18c3f3109cffa76e4369c810f82d36eb02c56af

stage2: save and restore parameters when resolving inline bodies

This caused zirParam instructions of parent blocks to be present in inline analyzed blocks, and so function prototypes declared in the inline blocks would also gain and add to the parameters in the parent block. Only block and block_inline are affected in this commit, as prototypes and declarations are always generated in block_inline. This might need to be resolved in a more general way at some point.

1 files changed, 18 insertions(+), 0 deletions(-)

src/Sema.zig+18
...@@ -940,6 +940,15 @@ pub fn analyzeBody(...@@ -940,6 +940,15 @@ pub fn analyzeBody(
940 const inst_data = datas[inst].pl_node;940 const inst_data = datas[inst].pl_node;
941 const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index);941 const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index);
942 const inline_body = sema.code.extra[extra.end..][0..extra.data.body_len];942 const inline_body = sema.code.extra[extra.end..][0..extra.data.body_len];
943 // If this block contains a function prototype, we need to reset the
944 // current list of parameters and restore it later.
945 // Note: this probably needs to be resolved in a more general manner.
946 const prev_params = block.params;
947 block.params = .{};
948 defer {
949 block.params.deinit(sema.gpa);
950 block.params = prev_params;
951 }
943 const break_inst = try sema.analyzeBody(block, inline_body);952 const break_inst = try sema.analyzeBody(block, inline_body);
944 const break_data = datas[break_inst].@"break";953 const break_data = datas[break_inst].@"break";
945 if (inst == break_data.block_inst) {954 if (inst == break_data.block_inst) {
...@@ -953,6 +962,15 @@ pub fn analyzeBody(...@@ -953,6 +962,15 @@ pub fn analyzeBody(
953 const inst_data = datas[inst].pl_node;962 const inst_data = datas[inst].pl_node;
954 const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index);963 const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index);
955 const inline_body = sema.code.extra[extra.end..][0..extra.data.body_len];964 const inline_body = sema.code.extra[extra.end..][0..extra.data.body_len];
965 // If this block contains a function prototype, we need to reset the
966 // current list of parameters and restore it later.
967 // Note: this probably needs to be resolved in a more general manner.
968 const prev_params = block.params;
969 block.params = .{};
970 defer {
971 block.params.deinit(sema.gpa);
972 block.params = prev_params;
973 }
956 const break_inst = try sema.analyzeBody(block, inline_body);974 const break_inst = try sema.analyzeBody(block, inline_body);
957 const break_data = datas[break_inst].@"break";975 const break_data = datas[break_inst].@"break";
958 if (inst == break_data.block_inst) {976 if (inst == break_data.block_inst) {