From ba82db12876da237d898a0c4750fbcd42ec24d30 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Fri, 7 Jul 2023 14:17:50 -0600 Subject: [PATCH] Add constructor tracking --- tool/cppcgen.cpp | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/tool/cppcgen.cpp b/tool/cppcgen.cpp index 216a8e7..1dd9952 100644 --- a/tool/cppcgen.cpp +++ b/tool/cppcgen.cpp @@ -20,6 +20,7 @@ #include // for cpp_namespace #include // for libclang_parser, libclang_compile_config, cpp_entity,... #include // for visit() +#include std::string CLASS_SUFFIX = "Ext"; std::string FUNC_SUFFIX = "Func"; @@ -603,21 +604,21 @@ void output_consdes_typedef(std::stringstream& out, const cppast::cpp_entity& e, out << ");\n"; } -void output_constructor(std::stringstream& out, std::string& var_init_str, std::string& all_params, std::vector constr_params, bool has_constructor, std::string& prefix, std::string& base_class, std::string& current_class) { - if (all_params.length() == 0 and constr_params.size() == 0) return; +void output_constructor(std::stringstream& out, std::string& var_init_str, std::string& all_params, cppast::detail::iteratable_intrusive_list constr_params, bool has_constructor, std::string& prefix, std::string& base_class, std::string& current_class) { + if (all_params.length() == 0 and constr_params.empty()) return; std::stringstream constr_param_init_stream; std::stringstream constr_param_stream; int i = 0; - for (auto param : constr_params) { + for (auto& param : constr_params) { constr_param_init_stream << (i ? ", " : ""); - constr_param_init_stream << cppast::to_string(param->type()) << " " << param->name(); + constr_param_init_stream << cppast::to_string(param.type()) << " " << param.name(); constr_param_stream << (i ? ", " : ""); - constr_param_stream << param->name(); + constr_param_stream << param.name(); i++; } - if (all_params.length() > 0 && constr_params.size() > 0) { + if (all_params.length() > 0 && !constr_params.empty()) { constr_param_init_stream << ", "; } @@ -730,7 +731,7 @@ void print_ast(std::ostream& out, const cppast::cpp_file& file, const cppast::cp out << "#ifndef " << guard_name << "\n"; out << "#define " << guard_name << "\n\n"; - out << "#include \n#include \n#include \n\n"; + out << "#include \n#include <" << include_path << ">\n#include \n#include \n\n"; /* out << "namespace wxname {\n#include_next <" << include_path << ">\n};\n\n"; */ @@ -745,6 +746,7 @@ void print_ast(std::ostream& out, const cppast::cpp_file& file, const cppast::cp bool has_constructor = false; int min_num_params = INT_MAX; std::vector constr_params; + std::vector constructors; std::map members; @@ -786,6 +788,7 @@ void print_ast(std::ostream& out, const cppast::cpp_file& file, const cppast::cp std::cerr << "\nCLASS: " << mf.name() << std::endl; for (auto& base : mf.bases()) { + if (base.type().kind() != cppast::cpp_type_kind::user_defined_t) continue; auto entity = idx.lookup(*static_cast(base.type()).entity().id().data()); std::cerr << "BASENAME: " << base.name() << std::endl; if (entity.has_value()) { @@ -794,11 +797,14 @@ void print_ast(std::ostream& out, const cppast::cpp_file& file, const cppast::cp } while (!stack.empty()) { auto entity = stack.back(); stack.pop_back(); + if (entity->kind() != cppast::cpp_entity_kind::class_t) continue; auto& entclass = static_cast(*entity); if (entclass.name().empty()) continue; std::cerr << "BASECLASS: " << entclass.name() << std::endl; cppast::visit(entclass, method_filter, [&](const cppast::cpp_entity& e, cppast::visitor_info info) { + if (e.kind() != cppast::cpp_entity_kind::member_function_t) + return true; if (!e.parent().has_value()) return true; if (e.parent().value().name() != entclass.name()) return true; auto& mf = static_cast(e); @@ -812,6 +818,9 @@ void print_ast(std::ostream& out, const cppast::cpp_file& file, const cppast::cp } ); for (auto& entbase : entclass.bases()) { + if (entbase.type().kind() != cppast::cpp_type_kind::user_defined_t) { + continue; + } auto ent = idx.lookup(*static_cast(entbase.type()).entity().id().data()); if (ent.has_value()) { stack.push_back(&ent.value()); @@ -855,7 +864,17 @@ void print_ast(std::ostream& out, const cppast::cpp_file& file, const cppast::cp param_str.pop_back(); param_str.pop_back(); } - output_constructor(current_class_stream, var_init_str, param_str, constr_params, has_constructor, prefix, base_class, current_class); + + if (constructors.empty()) { + cppast::detail::intrusive_list params; + cppast::detail::iteratable_intrusive_list emptyList(type_safe::ref(params)); + + output_constructor(current_class_stream, var_init_str, param_str, emptyList, false, prefix, base_class, current_class); + } + for (auto& fn : constructors) { + output_constructor(current_class_stream, var_init_str, param_str, fn->parameters(), true, prefix, base_class, current_class); + } + init_params.str(std::string()); // clear var_init.str(std::string()); // clear current_class_stream << "};\n\n"; @@ -875,6 +894,7 @@ void print_ast(std::ostream& out, const cppast::cpp_file& file, const cppast::cp min_num_params = INT_MAX; has_constructor = false; constr_params.clear(); + constructors.clear(); //std::cerr << "EXIT CLASS\n"; //std::cerr << current_class << "\n\n"; current_class = ""; @@ -889,6 +909,7 @@ void print_ast(std::ostream& out, const cppast::cpp_file& file, const cppast::cp if (e.kind() == cppast::cpp_entity_kind::constructor_t) { auto& mf = static_cast(e); + constructors.push_back(&mf); int num_params = 0; std::vector tempparams; has_constructor = true;