From 29ca948f09f40bc9db6232a92efa5ac44331ea28 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 26 Jun 2023 06:47:54 +0300 Subject: [PATCH] TL-generate: split write_tl to several functions. --- tdtl/td/tl/tl_generate.cpp | 207 +++++++++++++++++++------------------ 1 file changed, 109 insertions(+), 98 deletions(-) diff --git a/tdtl/td/tl/tl_generate.cpp b/tdtl/td/tl/tl_generate.cpp index 2b3cb3bc2..03dce1556 100644 --- a/tdtl/td/tl/tl_generate.cpp +++ b/tdtl/td/tl/tl_generate.cpp @@ -504,13 +504,8 @@ static void dfs_type(const tl_type *t, std::set &found, const TL_wr } } -void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) { - out.append(w.gen_output_begin()); - - std::size_t types_n = config.get_type_count(); - std::size_t functions_n = config.get_function_count(); - - for (std::size_t type = 0; type < types_n; type++) { +static void find_complex_types(const tl_config &config, const TL_writer &w) { + for (std::size_t type = 0; type < config.get_type_count(); type++) { tl_type *t = config.get_type_by_num(type); assert(t->constructors_num == t->constructors.size()); if (t->constructors_num == 0) { // built-in dummy types @@ -580,7 +575,7 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) { while (true) { bool found_complex = false; - for (std::size_t type = 0; type < types_n; type++) { + for (std::size_t type = 0; type < config.get_type_count(); type++) { tl_type *t = config.get_type_by_num(type); if (t->constructors_num == 0 || w.is_built_in_complex_type(t->name)) { // built-in dummy or complex types continue; @@ -603,37 +598,11 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) { break; } } +} - std::set request_types; - std::set result_types; - for (std::size_t function = 0; function < functions_n; function++) { - const tl_combinator *t = config.get_function_by_num(function); - dfs_combinator(t, request_types, w); - dfs_tree(t->result, result_types, w); - } - - // write forward declarations - for (std::size_t type = 0; type < types_n; type++) { - tl_type *t = config.get_type_by_num(type); - if (t->constructors_num == 0 || w.is_built_in_simple_type(t->name) || w.is_built_in_complex_type(t->name) || - (t->flags & FLAG_COMPLEX)) { // built-in or complex types - continue; - } - - assert(t->flags == 0); - - if (t->simple_constructors != 1) { - out.append(w.gen_forward_class_declaration(w.gen_class_name(t->name), true)); - } else { - for (std::size_t i = 0; i < t->constructors_num; i++) { - if (w.is_combinator_supported(t->constructors[i])) { - out.append(w.gen_forward_class_declaration(w.gen_class_name(t->constructors[i]->name), false)); - } - } - } - } - - // write base classes +static void write_base_object_classes(const tl_config &config, tl_outputer &out, + const std::set &request_types, + const std::set &result_types, const TL_writer &w) { std::vector empty_vars; for (int i = 0; i <= w.get_max_arity(); i++) { out.append(w.gen_class_begin(w.gen_base_type_class_name(i), w.gen_base_tl_class_name(), true, nullptr)); @@ -643,7 +612,7 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) { std::vector parsers = w.get_parsers(); for (std::size_t j = 0; j < parsers.size(); j++) { int case_count = 0; - for (std::size_t type = 0; type < types_n; type++) { + for (std::size_t type = 0; type < config.get_type_count(); type++) { tl_type *t = config.get_type_by_num(type); if (t->constructors_num == 0 || w.is_built_in_simple_type(t->name) || w.is_built_in_complex_type(t->name) || (t->flags & FLAG_COMPLEX)) { // built-in or complex types @@ -668,7 +637,7 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) { out.append(w.gen_fetch_function_begin(parsers[j], w.gen_base_type_class_name(i), w.gen_base_type_class_name(i), i, -1, empty_vars, -1)); out.append(w.gen_fetch_switch_begin()); - for (std::size_t type = 0; type < types_n; type++) { + for (std::size_t type = 0; type < config.get_type_count(); type++) { tl_type *t = config.get_type_by_num(type); if (t->constructors_num == 0 || w.is_built_in_simple_type(t->name) || w.is_built_in_complex_type(t->name) || (t->flags & FLAG_COMPLEX)) { // built-in or complex types @@ -693,7 +662,7 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) { for (std::size_t j = 0; j < additional_functions.size(); j++) { out.append(w.gen_additional_proxy_function_begin(additional_functions[j], NULL, w.gen_base_type_class_name(i), i, false)); - for (std::size_t type = 0; type < types_n; type++) { + for (std::size_t type = 0; type < config.get_type_count(); type++) { tl_type *t = config.get_type_by_num(type); if (t->constructors_num == 0 || w.is_built_in_simple_type(t->name) || w.is_built_in_complex_type(t->name) || (t->flags & FLAG_COMPLEX)) { // built-in or complex types @@ -728,70 +697,112 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) { out.append(w.gen_class_end()); } +} - { - out.append(w.gen_class_begin(w.gen_base_function_class_name(), w.gen_base_tl_class_name(), true, nullptr)); +static void write_base_function_class(const tl_config &config, tl_outputer &out, + const std::set &request_types, + const std::set &result_types, const TL_writer &w) { + out.append(w.gen_class_begin(w.gen_base_function_class_name(), w.gen_base_tl_class_name(), true, nullptr)); - out.append(w.gen_get_id(w.gen_base_function_class_name(), 0, true)); + out.append(w.gen_get_id(w.gen_base_function_class_name(), 0, true)); - std::vector parsers = w.get_parsers(); - for (std::size_t j = 0; j < parsers.size(); j++) { - if (w.get_parser_mode(-1) == TL_writer::Client) { - continue; - } - - out.append(w.gen_fetch_function_begin(parsers[j], w.gen_base_function_class_name(), - w.gen_base_function_class_name(), 0, -1, empty_vars, -1)); - out.append(w.gen_fetch_switch_begin()); - for (std::size_t function = 0; function < functions_n; function++) { - tl_combinator *t = config.get_function_by_num(function); - - if (w.is_combinator_supported(t)) { - out.append(w.gen_fetch_switch_case(t, 0)); - } - } - out.append(w.gen_fetch_switch_end()); - out.append(w.gen_fetch_function_end(false, -1, empty_vars, -1)); + std::vector empty_vars; + std::vector parsers = w.get_parsers(); + for (std::size_t j = 0; j < parsers.size(); j++) { + if (w.get_parser_mode(-1) == TL_writer::Client) { + continue; } - std::vector storers = w.get_storers(); - for (std::size_t j = 0; j < storers.size(); j++) { - if (w.get_storer_mode(-1) == TL_writer::Server) { - continue; + out.append(w.gen_fetch_function_begin(parsers[j], w.gen_base_function_class_name(), + w.gen_base_function_class_name(), 0, -1, empty_vars, -1)); + out.append(w.gen_fetch_switch_begin()); + for (std::size_t function = 0; function < config.get_function_count(); function++) { + tl_combinator *t = config.get_function_by_num(function); + + if (w.is_combinator_supported(t)) { + out.append(w.gen_fetch_switch_case(t, 0)); } - - out.append(w.gen_store_function_begin(storers[j], w.gen_base_function_class_name(), 0, empty_vars, -1)); - out.append(w.gen_store_function_end(empty_vars, -1)); } - - for (std::size_t j = 0; j < parsers.size(); j++) { - if (w.get_parser_mode(-1) == TL_writer::Server) { - continue; - } - - out.append(w.gen_fetch_function_result_any_begin(parsers[j], w.gen_base_function_class_name(), true)); - out.append(w.gen_fetch_function_result_any_end(true)); - } - - std::vector additional_functions = w.get_additional_functions(); - for (std::size_t j = 0; j < additional_functions.size(); j++) { - out.append(w.gen_additional_proxy_function_begin(additional_functions[j], NULL, w.gen_base_function_class_name(), - 0, true)); - for (std::size_t function = 0; function < functions_n; function++) { - tl_combinator *t = config.get_function_by_num(function); - - if (w.is_combinator_supported(t)) { - out.append(w.gen_additional_proxy_function_case(additional_functions[j], NULL, t, 0, true)); - } - } - - out.append(w.gen_additional_proxy_function_end(additional_functions[j], NULL, true)); - } - - out.append(w.gen_class_end()); + out.append(w.gen_fetch_switch_end()); + out.append(w.gen_fetch_function_end(false, -1, empty_vars, -1)); } - for (std::size_t type = 0; type < types_n; type++) { + std::vector storers = w.get_storers(); + for (std::size_t j = 0; j < storers.size(); j++) { + if (w.get_storer_mode(-1) == TL_writer::Server) { + continue; + } + + out.append(w.gen_store_function_begin(storers[j], w.gen_base_function_class_name(), 0, empty_vars, -1)); + out.append(w.gen_store_function_end(empty_vars, -1)); + } + + for (std::size_t j = 0; j < parsers.size(); j++) { + if (w.get_parser_mode(-1) == TL_writer::Server) { + continue; + } + + out.append(w.gen_fetch_function_result_any_begin(parsers[j], w.gen_base_function_class_name(), true)); + out.append(w.gen_fetch_function_result_any_end(true)); + } + + std::vector additional_functions = w.get_additional_functions(); + for (std::size_t j = 0; j < additional_functions.size(); j++) { + out.append(w.gen_additional_proxy_function_begin(additional_functions[j], NULL, w.gen_base_function_class_name(), 0, + true)); + for (std::size_t function = 0; function < config.get_function_count(); function++) { + tl_combinator *t = config.get_function_by_num(function); + + if (w.is_combinator_supported(t)) { + out.append(w.gen_additional_proxy_function_case(additional_functions[j], NULL, t, 0, true)); + } + } + + out.append(w.gen_additional_proxy_function_end(additional_functions[j], NULL, true)); + } + + out.append(w.gen_class_end()); +} + +void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) { + find_complex_types(config, w); + + out.append(w.gen_output_begin()); + + std::set request_types; + std::set result_types; + for (std::size_t function = 0; function < config.get_function_count(); function++) { + const tl_combinator *t = config.get_function_by_num(function); + dfs_combinator(t, request_types, w); + dfs_tree(t->result, result_types, w); + } + + // write forward declarations + for (std::size_t type = 0; type < config.get_type_count(); type++) { + tl_type *t = config.get_type_by_num(type); + if (t->constructors_num == 0 || w.is_built_in_simple_type(t->name) || w.is_built_in_complex_type(t->name) || + (t->flags & FLAG_COMPLEX)) { // built-in or complex types + continue; + } + + assert(t->flags == 0); + + if (t->simple_constructors != 1) { + out.append(w.gen_forward_class_declaration(w.gen_class_name(t->name), true)); + } else { + for (std::size_t i = 0; i < t->constructors_num; i++) { + if (w.is_combinator_supported(t->constructors[i])) { + out.append(w.gen_forward_class_declaration(w.gen_class_name(t->constructors[i]->name), false)); + } + } + } + } + + write_base_object_classes(config, out, request_types, result_types, w); + + write_base_function_class(config, out, request_types, result_types, w); + + for (std::size_t type = 0; type < config.get_type_count(); type++) { tl_type *t = config.get_type_by_num(type); if (t->constructors_num == 0 || w.is_built_in_simple_type(t->name) || w.is_built_in_complex_type(t->name)) { // built-in dummy or complex types @@ -806,7 +817,7 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) { write_class(out, t, request_types, result_types, w); } - for (std::size_t function = 0; function < functions_n; function++) { + for (std::size_t function = 0; function < config.get_function_count(); function++) { tl_combinator *t = config.get_function_by_num(function); if (!w.is_combinator_supported(t)) { // std::fprintf(stderr, "Function %s is too hard to store\n", t->name.c_str()); @@ -817,7 +828,7 @@ void write_tl(const tl_config &config, tl_outputer &out, const TL_writer &w) { } out.append(w.gen_output_end()); - for (std::size_t type = 0; type < types_n; type++) { + for (std::size_t type = 0; type < config.get_type_count(); type++) { tl_type *t = config.get_type_by_num(type); if (t->flags & FLAG_COMPLEX) { t->flags &= ~FLAG_COMPLEX; // remove temporary flag