From 1c6366eb9c2f0786888aabab92b46a112529531c Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 10 Aug 2022 14:05:11 +0300 Subject: [PATCH] cgen: fix generated any/filter/all methods for shared array fields --- vlib/v/gen/c/array.v | 97 ++++++------------- .../shared_array_any_filter_map_all_test.v | 34 +++++++ 2 files changed, 66 insertions(+), 65 deletions(-) create mode 100644 vlib/v/tests/shared_array_any_filter_map_all_test.v diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 0b523d68f9..903883a034 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -336,30 +336,7 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) { } g.empty_line = true noscan := g.check_noscan(ret_info.elem_type) - g.writeln('$ret_typ $tmp = {0};') - has_infix_left_var_name := g.infix_left_var_name.len > 0 - if has_infix_left_var_name { - g.writeln('if ($g.infix_left_var_name) {') - g.infix_left_var_name = '' - g.indent++ - } - left_type := if node.left_type.has_flag(.shared_f) { - node.left_type.clear_flag(.shared_f).deref() - } else if node.left_type.is_ptr() { - node.left_type.deref() - } else { - node.left_type - } - g.write('${g.typ(left_type)} ${tmp}_orig = ') - if !node.left_type.has_flag(.shared_f) && node.left_type.is_ptr() { - g.write('*') - } - g.expr(node.left) - if node.left_type.has_flag(.shared_f) { - g.write('->val') - } - g.writeln(';') - g.writeln('int ${tmp}_len = ${tmp}_orig.len;') + has_infix_left_var_name := g.write_prepared_tmp_value(tmp, node, ret_typ, '{0}') g.writeln('$tmp = __new_array${noscan}(0, ${tmp}_len, sizeof($ret_elem_type));\n') i := g.new_tmp_var() g.writeln('for (int $i = 0; $i < ${tmp}_len; ++$i) {') @@ -554,25 +531,7 @@ fn (mut g Gen) gen_array_filter(node ast.CallExpr) { elem_type_str := g.typ(info.elem_type) g.empty_line = true noscan := g.check_noscan(info.elem_type) - g.writeln('$styp $tmp = {0};') - has_infix_left_var_name := g.infix_left_var_name.len > 0 - if has_infix_left_var_name { - g.writeln('if ($g.infix_left_var_name) {') - g.infix_left_var_name = '' - g.indent++ - } - left_type := if node.left_type.has_flag(.shared_f) { - node.left_type.clear_flag(.shared_f).deref() - } else { - node.left_type - } - g.write('${g.typ(left_type)} ${tmp}_orig = ') - g.expr(node.left) - if node.left_type.has_flag(.shared_f) { - g.write('->val') - } - g.writeln(';') - g.writeln('int ${tmp}_len = ${tmp}_orig.len;') + has_infix_left_var_name := g.write_prepared_tmp_value(tmp, node, styp, '{0}') g.writeln('$tmp = __new_array${noscan}(0, ${tmp}_len, sizeof($elem_type_str));\n') i := g.new_tmp_var() g.writeln('for (int $i = 0; $i < ${tmp}_len; ++$i) {') @@ -930,17 +889,7 @@ fn (mut g Gen) gen_array_any(node ast.CallExpr) { // styp := g.typ(node.return_type) elem_type_str := g.typ(info.elem_type) g.empty_line = true - g.writeln('bool $tmp = false;') - has_infix_left_var_name := g.infix_left_var_name.len > 0 - if has_infix_left_var_name { - g.writeln('if ($g.infix_left_var_name) {') - g.infix_left_var_name = '' - g.indent++ - } - g.write('${g.typ(node.left_type)} ${tmp}_orig = ') - g.expr(node.left) - g.writeln(';') - g.writeln('int ${tmp}_len = ${tmp}_orig.len;') + has_infix_left_var_name := g.write_prepared_tmp_value(tmp, node, 'bool', 'false') i := g.new_tmp_var() g.writeln('for (int $i = 0; $i < ${tmp}_len; ++$i) {') g.indent++ @@ -1014,17 +963,7 @@ fn (mut g Gen) gen_array_all(node ast.CallExpr) { // styp := g.typ(node.return_type) elem_type_str := g.typ(info.elem_type) g.empty_line = true - g.writeln('bool $tmp = true;') - has_infix_left_var_name := g.infix_left_var_name.len > 0 - if has_infix_left_var_name { - g.writeln('if ($g.infix_left_var_name) {') - g.infix_left_var_name = '' - g.indent++ - } - g.write('${g.typ(node.left_type)} ${tmp}_orig = ') - g.expr(node.left) - g.writeln(';') - g.writeln('int ${tmp}_len = ${tmp}_orig.len;') + has_infix_left_var_name := g.write_prepared_tmp_value(tmp, node, 'bool', 'true') i := g.new_tmp_var() g.writeln('for (int $i = 0; $i < ${tmp}_len; ++$i) {') g.indent++ @@ -1088,3 +1027,31 @@ fn (mut g Gen) gen_array_all(node ast.CallExpr) { } g.write(tmp) } + +fn (mut g Gen) write_prepared_tmp_value(tmp string, node &ast.CallExpr, tmp_stype string, initial_value string) bool { + g.writeln('$tmp_stype $tmp = $initial_value;') + has_infix_left_var_name := g.infix_left_var_name.len > 0 + if has_infix_left_var_name { + g.writeln('if ($g.infix_left_var_name) {') + g.infix_left_var_name = '' + g.indent++ + } + left_type := if node.left_type.has_flag(.shared_f) { + node.left_type.clear_flag(.shared_f).deref() + } else if node.left_type.is_ptr() { + node.left_type.deref() + } else { + node.left_type + } + g.write('${g.typ(left_type)} ${tmp}_orig = ') + if !node.left_type.has_flag(.shared_f) && node.left_type.is_ptr() { + g.write('*') + } + g.expr(node.left) + if node.left_type.has_flag(.shared_f) { + g.write('->val') + } + g.writeln(';') + g.writeln('int ${tmp}_len = ${tmp}_orig.len;') + return has_infix_left_var_name +} diff --git a/vlib/v/tests/shared_array_any_filter_map_all_test.v b/vlib/v/tests/shared_array_any_filter_map_all_test.v new file mode 100644 index 0000000000..9d39ac5dc5 --- /dev/null +++ b/vlib/v/tests/shared_array_any_filter_map_all_test.v @@ -0,0 +1,34 @@ +module main + +import time + +struct Alarms { +mut: + times shared []time.Time +} + +fn (shared alarms Alarms) add(alarm time.Time) { + lock alarms.times { + println(alarms.times.filter(it == alarm)) + println(alarms.times.all(it == alarm)) + println(alarms.times.map(it.year)) + //// + if alarms.times.any(it == alarm) { + return + } + alarms.times << alarm + } +} + +fn test_generated_shared_array_methods() { + shared alarms := Alarms{ + times: []time.Time{cap: 10} + } + utc := time.utc() + alarms.add(utc) + alarms.add(utc) + lock alarms.times { + println(alarms.times) + assert alarms.times.len == 1, 'length should be exactly 1' + } +}