Add get_children_types helper.

This commit is contained in:
levlam 2023-06-26 15:56:15 +03:00
parent 8f9a4dcaa0
commit 6e543d3cdd

View File

@ -50,14 +50,13 @@ static bool is_reachable_for_storer(int storer_type, const std::string &name,
return true; return true;
} }
static void write_forward_declaration(tl_outputer &out, const tl_tree *tree, const TL_writer &w, static void get_children_types(const tl_tree *tree, const TL_writer &w, std::map<std::string, bool> &children_types) {
std::set<std::string> &existing_forward_declarations) {
if (tree->get_type() != NODE_TYPE_TYPE) { if (tree->get_type() != NODE_TYPE_TYPE) {
return; return;
} }
const tl_tree_type *tree_type = static_cast<const tl_tree_type *>(tree); const tl_tree_type *tree_type = static_cast<const tl_tree_type *>(tree);
for (std::size_t i = 0; i < tree_type->children.size(); i++) { 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; const tl_type *t = tree_type->type;
@ -69,18 +68,21 @@ static void write_forward_declaration(tl_outputer &out, const tl_tree *tree, con
assert(t->flags == 0); assert(t->flags == 0);
if (t->simple_constructors != 1) { if (t->simple_constructors != 1) {
if (existing_forward_declarations.insert(w.gen_class_name(t->name)).second) { children_types.emplace(t->name, true);
out.append(w.gen_forward_class_declaration(w.gen_class_name(t->name), true));
}
} else { } else {
for (std::size_t i = 0; i < t->constructors_num; i++) { for (std::size_t i = 0; i < t->constructors_num; i++) {
if (w.is_combinator_supported(t->constructors[i])) { if (w.is_combinator_supported(t->constructors[i])) {
if (existing_forward_declarations.insert(w.gen_class_name(t->constructors[i]->name)).second) { children_types.emplace(t->constructors[i]->name, false);
out.append(w.gen_forward_class_declaration(w.gen_class_name(t->constructors[i]->name), false));
} }
} }
} }
} }
static void write_forward_declarations(tl_outputer &out, const std::map<std::string, bool> &children_types,
const TL_writer &w) {
for (std::map<std::string, bool>::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, static void write_class_constructor(tl_outputer &out, const tl_combinator *t, const std::string &class_name,
@ -276,11 +278,12 @@ static void write_function(tl_outputer &out, const tl_combinator *t, const std::
const std::set<std::string> &result_types, const TL_writer &w) { const std::set<std::string> &result_types, const TL_writer &w) {
assert(w.is_combinator_supported(t)); assert(w.is_combinator_supported(t));
std::set<std::string> existing_forward_declarations; std::map<std::string, bool> children_types;
for (std::size_t i = 0; i < t->args.size(); i++) { 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); 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::set<std::
const std::string base_class = w.gen_base_type_class_name(t->arity); const std::string base_class = w.gen_base_type_class_name(t->arity);
const std::string class_name = w.gen_class_name(t->name); const std::string class_name = w.gen_class_name(t->name);
std::set<std::string> existing_forward_declarations; std::map<std::string, bool> children_types;
for (std::size_t i = 0; i < t->constructors_num; i++) { for (std::size_t i = 0; i < t->constructors_num; i++) {
if (w.is_combinator_supported(t->constructors[i])) { if (w.is_combinator_supported(t->constructors[i])) {
for (std::size_t j = 0; j < t->constructors[i]->args.size(); j++) { 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<var_description> empty_vars; std::vector<var_description> empty_vars;
bool optimize_one_constructor = (t->simple_constructors == 1); bool optimize_one_constructor = (t->simple_constructors == 1);