diff --git a/tdtl/td/tl/tl_generate.cpp b/tdtl/td/tl/tl_generate.cpp index 5222dd318..7a61d32f8 100644 --- a/tdtl/td/tl/tl_generate.cpp +++ b/tdtl/td/tl/tl_generate.cpp @@ -50,14 +50,13 @@ static bool is_reachable_for_storer(int storer_type, const std::string &name, return true; } -static void write_forward_declaration(tl_outputer &out, const tl_tree *tree, const TL_writer &w, - std::set &existing_forward_declarations) { +static void get_children_types(const tl_tree *tree, const TL_writer &w, std::map &children_types) { if (tree->get_type() != NODE_TYPE_TYPE) { return; } const tl_tree_type *tree_type = static_cast(tree); for (std::size_t i = 0; i < tree_type->children.size(); i++) { - write_forward_declaration(out, tree_type->children[i], w, existing_forward_declarations); + get_children_types(tree_type->children[i], w, children_types); } const tl_type *t = tree_type->type; @@ -69,20 +68,23 @@ static void write_forward_declaration(tl_outputer &out, const tl_tree *tree, con assert(t->flags == 0); if (t->simple_constructors != 1) { - if (existing_forward_declarations.insert(w.gen_class_name(t->name)).second) { - out.append(w.gen_forward_class_declaration(w.gen_class_name(t->name), true)); - } + children_types.emplace(t->name, true); } else { for (std::size_t i = 0; i < t->constructors_num; i++) { if (w.is_combinator_supported(t->constructors[i])) { - if (existing_forward_declarations.insert(w.gen_class_name(t->constructors[i]->name)).second) { - out.append(w.gen_forward_class_declaration(w.gen_class_name(t->constructors[i]->name), false)); - } + children_types.emplace(t->constructors[i]->name, false); } } } } +static void write_forward_declarations(tl_outputer &out, const std::map &children_types, + const TL_writer &w) { + for (std::map::const_iterator it = children_types.begin(); it != children_types.end(); ++it) { + out.append(w.gen_forward_class_declaration(w.gen_class_name(it->first), it->second)); + } +} + static void write_class_constructor(tl_outputer &out, const tl_combinator *t, const std::string &class_name, bool is_default, const TL_writer &w) { // std::fprintf(stderr, "Gen constructor %s\n", class_name.c_str()); @@ -276,11 +278,12 @@ static void write_function(tl_outputer &out, const tl_combinator *t, const std:: const std::set &result_types, const TL_writer &w) { assert(w.is_combinator_supported(t)); - std::set existing_forward_declarations; + std::map children_types; for (std::size_t i = 0; i < t->args.size(); i++) { - write_forward_declaration(out, t->args[i].type, w, existing_forward_declarations); + get_children_types(t->args[i].type, w, children_types); } - write_forward_declaration(out, t->result, w, existing_forward_declarations); + get_children_types(t->result, w, children_types); + write_forward_declarations(out, children_types, w); std::string class_name = w.gen_class_name(t->name); @@ -422,14 +425,15 @@ static void write_class(tl_outputer &out, const tl_type *t, const std::setarity); const std::string class_name = w.gen_class_name(t->name); - std::set existing_forward_declarations; + std::map children_types; for (std::size_t i = 0; i < t->constructors_num; i++) { if (w.is_combinator_supported(t->constructors[i])) { for (std::size_t j = 0; j < t->constructors[i]->args.size(); j++) { - write_forward_declaration(out, t->constructors[i]->args[j].type, w, existing_forward_declarations); + get_children_types(t->constructors[i]->args[j].type, w, children_types); } } } + write_forward_declarations(out, children_types, w); std::vector empty_vars; bool optimize_one_constructor = (t->simple_constructors == 1);