diff --git a/compiler/code-gen/vertex-compiler.cpp b/compiler/code-gen/vertex-compiler.cpp index 2a94095ccf..18c84a7652 100644 --- a/compiler/code-gen/vertex-compiler.cpp +++ b/compiler/code-gen/vertex-compiler.cpp @@ -1149,8 +1149,7 @@ void compile_switch_str(VertexAdaptor root, CodeGenerator &W) { auto temp_var_matched_with_a_case = root->matched_with_one_case(); // because we checked types before in case of match - const auto *const convert_function = root->is_match ? "" : "f$strval"; - + const char *convert_function = root->is_match ? "" : "f$strval"; W << BEGIN; W << temp_var_strval_of_condition << " = " << convert_function << "(" << root->condition() << ");" << NL; @@ -1197,7 +1196,7 @@ void compile_switch_str(VertexAdaptor root, CodeGenerator &W) { void compile_switch_int(VertexAdaptor root, CodeGenerator &W) { // because we checked types before in case of match - const auto *const convert_function = root->is_match ? "" : "f$intval"; + const char *convert_function = root->is_match ? "" : "f$intval"; W << "switch (" << convert_function << " (" << root->condition() << "))" << BEGIN; W << "static_cast(" << root->condition_on_switch() << ");" << NL; @@ -1213,8 +1212,7 @@ void compile_switch_int(VertexAdaptor root, CodeGenerator &W) { if (val->type() == op_int_const) { const std::string &str = val.as()->str_val; W << str; - const std::string op = root->is_match ? "Match" : "Switch"; - kphp_error(used.insert(str).second, fmt_format("{}: repeated cases found [{}]", op, str)); + kphp_error(used.insert(str).second, fmt_format("{}: repeated cases found [{}]", root->is_match ? "Match" : "Switch", str)); } else { kphp_assert(VertexUtil::is_const_int(val)); W << val; @@ -1236,7 +1234,7 @@ void compile_switch_var(VertexAdaptor root, CodeGenerator &W) { auto temp_var_condition_on_switch = root->condition_on_switch(); auto temp_var_matched_with_a_case = root->matched_with_one_case(); - const auto *const eq_function = root->is_match ? "equals" : "eq2"; + const char *eq_function = root->is_match ? "equals" : "eq2"; W << "do " << BEGIN; W << temp_var_condition_on_switch << " = " << root->condition() << ";" << NL; @@ -1287,15 +1285,14 @@ void compile_switch(VertexAdaptor root, CodeGenerator &W) { for (auto one_case : root->cases()) { if (one_case->type() == op_default) { - const std::string op = root->is_match ? "Match" : "Switch"; - kphp_error_return(!has_default, op + ": several `default` cases found"); + kphp_error_return(!has_default, fmt_format("%s: several `default` cases found", root->is_match ? "Match" : "Switch")); has_default = true; continue; } } if (root->is_match) { - const auto *const cond_type = tinf::get_type(root->condition()); + const TypeData *cond_type = tinf::get_type(root->condition()); if (!cond_type) { compile_switch_var(root, W); return; diff --git a/compiler/gentree.cpp b/compiler/gentree.cpp index 6a2b2764b0..6dfa6b0445 100644 --- a/compiler/gentree.cpp +++ b/compiler/gentree.cpp @@ -1228,7 +1228,7 @@ VertexAdaptor GenTree::get_switch() { } -VertexAdaptor GenTree::get_match() { +VertexAdaptor GenTree::get_match() { const auto location = auto_location(); next_cur(); CE(expect(tok_oppar, "'('")); @@ -1248,8 +1248,7 @@ VertexAdaptor GenTree::get_match() { if (cur->type() == tok_default) { cases.emplace_back(get_match_default()); - } - else { + } else { cases.emplace_back(get_match_case()); } kphp_assert_msg(cases.back(), "Invalid 'match' case!"); @@ -1257,7 +1256,7 @@ VertexAdaptor GenTree::get_match() { CE(expect(tok_clbrc, "'}'")); - return VertexAdaptor::create(match_condition, std::move(cases)).set_location(location); + return VertexAdaptor::create(match_condition, std::move(cases)).set_location(location); } VertexAdaptor GenTree::get_match_case() { @@ -1281,12 +1280,10 @@ VertexAdaptor GenTree::get_match_case() { CE(expect(tok_double_arrow, "'=>'")); kphp_error(!arms.empty(), "Expected expression before '=>'"); result = get_expression(); - } - else if (const auto double_arrow = cur_expr.try_as()) { + } else if (const auto double_arrow = cur_expr.try_as()) { arms.emplace_back(double_arrow->key()); result = double_arrow->value(); - } - else { + } else { kphp_fail_msg("Ivalid syntax of 'match' cases!"); } @@ -1304,6 +1301,7 @@ VertexAdaptor GenTree::get_match_default() { CE(expect(tok_double_arrow, "'=>'")); return VertexAdaptor::create(get_expression()).set_location(location); } + VertexAdaptor GenTree::get_shape() { auto location = auto_location(); diff --git a/compiler/gentree.h b/compiler/gentree.h index 79939f91ea..5109a2add2 100644 --- a/compiler/gentree.h +++ b/compiler/gentree.h @@ -100,7 +100,7 @@ class GenTree { VertexAdaptor get_for(); VertexAdaptor get_do(); VertexAdaptor get_switch(); - VertexAdaptor get_match(); + VertexAdaptor get_match(); VertexAdaptor get_match_case(); VertexAdaptor get_match_default(); VertexAdaptor get_shape(); diff --git a/compiler/pipes/collect-main-edges.cpp b/compiler/pipes/collect-main-edges.cpp index 33e9291fe4..3d736ee5b8 100644 --- a/compiler/pipes/collect-main-edges.cpp +++ b/compiler/pipes/collect-main-edges.cpp @@ -25,9 +25,9 @@ namespace { SwitchKind get_switch_kind(VertexAdaptor s) { int num_const_int_cases = 0; - int num_const_string_cases = 0; + int num_const_non_numeric_string_cases = 0; int num_value_cases = 0; - int num_const_total_string_cases = 0; + int num_const_string_cases = 0; for (auto one_case : s->cases()) { if (one_case->type() == op_default) { @@ -40,11 +40,11 @@ SwitchKind get_switch_kind(VertexAdaptor s) { num_const_int_cases++; } else if (auto as_string = val.try_as()) { // In case of op_switch node that was generated from op_match - num_const_total_string_cases++; + num_const_string_cases++; // PHP would use a numerical comparison for strings that look like a number, // we shouldn't rewrite these switches as a string-only switch if (!php_is_numeric(as_string->str_val.data())) { - num_const_string_cases++; + num_const_non_numeric_string_cases++; } } } @@ -52,10 +52,10 @@ SwitchKind get_switch_kind(VertexAdaptor s) { if (num_value_cases == 0) { return SwitchKind::EmptySwitch; } - if (s->is_match && num_const_total_string_cases == num_value_cases) { + if (s->is_match && num_const_string_cases == num_value_cases) { return SwitchKind::StringSwitch; } - if (num_const_string_cases == num_value_cases) { + if (num_const_non_numeric_string_cases == num_value_cases) { return SwitchKind::StringSwitch; } if (num_const_int_cases == num_value_cases) { @@ -419,8 +419,7 @@ void CollectMainEdgesPass::on_switch(VertexAdaptor switch_op) { create_set(as_lvalue(switch_op->condition_on_switch()->var_id), as_case->expr()); } } - } - else if (switch_op->kind == SwitchKind::IntSwitch) { + } else if (switch_op->kind == SwitchKind::IntSwitch) { // in case of int-only switch, condition var is discarded, so there is // no real need in trying to insert an assignment node here create_type_assign(as_lvalue(switch_op->condition_on_switch()), TypeData::get_type(tp_int)); diff --git a/compiler/pipes/gen-tree-postprocess.cpp b/compiler/pipes/gen-tree-postprocess.cpp index bd5d0c4610..e2aae3bab4 100644 --- a/compiler/pipes/gen-tree-postprocess.cpp +++ b/compiler/pipes/gen-tree-postprocess.cpp @@ -4,16 +4,13 @@ #include "compiler/pipes/gen-tree-postprocess.h" -#include "common/algorithms/find.h" +#include + #include "compiler/compiler-core.h" -#include "compiler/data/class-data.h" #include "compiler/data/lib-data.h" #include "compiler/data/src-file.h" -#include "compiler/data/vertex-adaptor.h" #include "compiler/name-gen.h" -#include "compiler/vertex-meta_op_base.h" #include "compiler/vertex-util.h" -#include namespace { template @@ -252,7 +249,7 @@ VertexPtr GenTreePostprocessPass::on_exit_vertex(VertexPtr root) { return convert_array_with_spread_operators(array); } - if (auto match = root.try_as()) { + if (auto match = root.try_as()) { return convert_match(match); } @@ -321,7 +318,7 @@ VertexPtr GenTreePostprocessPass::convert_array_with_spread_operators(VertexAdap return call; } -VertexPtr GenTreePostprocessPass::convert_match(VertexAdaptor match_vertex) { +VertexPtr GenTreePostprocessPass::convert_match(VertexAdaptor match_vertex) { auto gen_superlocal = [&](const std::string& name_prefix) { auto v = VertexAdaptor::create().set_location(match_vertex); v->str_val = gen_unique_name(name_prefix); diff --git a/compiler/pipes/gen-tree-postprocess.h b/compiler/pipes/gen-tree-postprocess.h index 87cf549f5b..d22180247b 100644 --- a/compiler/pipes/gen-tree-postprocess.h +++ b/compiler/pipes/gen-tree-postprocess.h @@ -24,6 +24,6 @@ class GenTreePostprocessPass final : public FunctionPassBase { // converts the spread operator (...$a) to a call to the array_merge_spread function static VertexPtr convert_array_with_spread_operators(VertexAdaptor array_vertex); // converts match to a statement expression that contains a switch operator and temporary variable - static VertexPtr convert_match(VertexAdaptor match_vertex); + static VertexPtr convert_match(VertexAdaptor match_vertex); }; diff --git a/compiler/vertex-desc.json b/compiler/vertex-desc.json index c8cbb8818c..3bb6794a9f 100644 --- a/compiler/vertex-desc.json +++ b/compiler/vertex-desc.json @@ -766,7 +766,7 @@ "sons": { "condition": 0 }, - "name": "op_match_proxy", + "name": "op_match", "base_name": "meta_op_cycle", "props": { "str": "match",