Skip to content

CPP: Fix list repeat issue 2221 #2228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 2 additions & 49 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
{
public:

std::unique_ptr<CUtils::CUtilFunctions> c_utils_functions;

int counter;

ASRToCVisitor(diag::Diagnostics &diag, CompilerOptions &co,
int64_t default_lower_bound)
: BaseCCPPVisitor(diag, co.platform, co, false, false, true, default_lower_bound),
c_utils_functions{std::make_unique<CUtils::CUtilFunctions>()},
counter{0} {
}

Expand Down Expand Up @@ -602,12 +599,6 @@ R"(

std::string indent(indentation_level * indentation_spaces, ' ');
std::string tab(indentation_spaces, ' ');
std::string strcat_def = "";
strcat_def += indent + "char* " + global_scope->get_unique_name("strcat_", false) + "(char* x, char* y) {\n";
strcat_def += indent + tab + "char* str_tmp = (char*) malloc((strlen(x) + strlen(y) + 2) * sizeof(char));\n";
strcat_def += indent + tab + "strcpy(str_tmp, x);\n";
strcat_def += indent + tab + "return strcat(str_tmp, y);\n";
strcat_def += indent + "}\n\n";

std::string unit_src_tmp;
for (auto &item : x.m_global_scope->get_scope()) {
Expand Down Expand Up @@ -700,48 +691,10 @@ R"(
unit_src += src;
}
}
std::string to_include = "";
for (auto &s: user_defines) {
to_include += "#define " + s + "\n";
}
for (auto &s: headers) {
to_include += "#include <" + s + ">\n";
}
for (auto &s: user_headers) {
to_include += "#include \"" + s + "\"\n";
}
if( c_ds_api->get_func_decls().size() > 0 ) {
array_types_decls += "\n" + c_ds_api->get_func_decls() + "\n";
}
if( c_utils_functions->get_util_func_decls().size() > 0 ) {
array_types_decls += "\n" + c_utils_functions->get_util_func_decls() + "\n";
}
std::string ds_funcs_defined = "";
if( c_ds_api->get_generated_code().size() > 0 ) {
ds_funcs_defined = "\n" + c_ds_api->get_generated_code() + "\n";
}
std::string util_funcs_defined = "";
if( c_utils_functions->get_generated_code().size() > 0 ) {
util_funcs_defined = "\n" + c_utils_functions->get_generated_code() + "\n";
}
if( bind_py_utils_functions->get_util_func_decls().size() > 0 ) {
array_types_decls += "\n" + bind_py_utils_functions->get_util_func_decls() + "\n";
}
if( bind_py_utils_functions->get_generated_code().size() > 0 ) {
util_funcs_defined = "\n" + bind_py_utils_functions->get_generated_code() + "\n";
}
if( is_string_concat_present ) {
head += strcat_def;
}

// Include dimension_descriptor definition that is used by array types
if (array_types_decls.size() != 0) {
array_types_decls.insert(0, "struct dimension_descriptor\n"
"{\n int32_t lower_bound, length;\n};\n");
}
forward_decl_functions += "\n\n";
src = to_include + head + array_types_decls + forward_decl_functions + unit_src +
ds_funcs_defined + util_funcs_defined;
src = get_final_combined_src(head, unit_src);

if (!emit_headers.empty()) {
std::string to_includes_1 = "";
for (auto &s: headers) {
Expand Down
54 changes: 53 additions & 1 deletion src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
std::string from_std_vector_helper;

std::unique_ptr<CCPPDSUtils> c_ds_api;
std::unique_ptr<CUtils::CUtilFunctions> c_utils_functions;
std::unique_ptr<BindPyUtils::BindPyUtilFunctions> bind_py_utils_functions;
std::string const_name;
size_t const_vars_count;
Expand Down Expand Up @@ -185,12 +186,63 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
gen_stdstring{gen_stdstring}, gen_stdcomplex{gen_stdcomplex},
is_c{is_c}, global_scope{nullptr}, lower_bound{default_lower_bound},
template_number{0}, c_ds_api{std::make_unique<CCPPDSUtils>(is_c, platform)},
c_utils_functions{std::make_unique<CUtils::CUtilFunctions>()},
bind_py_utils_functions{std::make_unique<BindPyUtils::BindPyUtilFunctions>()},
const_name{"constname"},
const_vars_count{0}, loop_end_count{0}, bracket_open{0},
is_string_concat_present{false} {
}

std::string get_final_combined_src(std::string head, std::string unit_src) {
std::string to_include = "";
for (auto &s: user_defines) {
to_include += "#define " + s + "\n";
}
for (auto &s: headers) {
to_include += "#include <" + s + ">\n";
}
for (auto &s: user_headers) {
to_include += "#include \"" + s + "\"\n";
}
if( c_ds_api->get_func_decls().size() > 0 ) {
array_types_decls += "\n" + c_ds_api->get_func_decls() + "\n";
}
if( c_utils_functions->get_util_func_decls().size() > 0 ) {
array_types_decls += "\n" + c_utils_functions->get_util_func_decls() + "\n";
}
std::string ds_funcs_defined = "";
if( c_ds_api->get_generated_code().size() > 0 ) {
ds_funcs_defined = "\n" + c_ds_api->get_generated_code() + "\n";
}
std::string util_funcs_defined = "";
if( c_utils_functions->get_generated_code().size() > 0 ) {
util_funcs_defined = "\n" + c_utils_functions->get_generated_code() + "\n";
}
if( bind_py_utils_functions->get_util_func_decls().size() > 0 ) {
array_types_decls += "\n" + bind_py_utils_functions->get_util_func_decls() + "\n";
}
if( bind_py_utils_functions->get_generated_code().size() > 0 ) {
util_funcs_defined = "\n" + bind_py_utils_functions->get_generated_code() + "\n";
}
if( is_string_concat_present ) {
std::string strcat_def = "";
strcat_def += " char* " + global_scope->get_unique_name("strcat_", false) + "(char* x, char* y) {\n";
strcat_def += " char* str_tmp = (char*) malloc((strlen(x) + strlen(y) + 2) * sizeof(char));\n";
strcat_def += " strcpy(str_tmp, x);\n";
strcat_def += " return strcat(str_tmp, y);\n";
strcat_def += " }\n\n";
head += strcat_def;
}

// Include dimension_descriptor definition that is used by array types
if (array_types_decls.size() != 0) {
array_types_decls = "\nstruct dimension_descriptor\n"
"{\n int32_t lower_bound, length;\n};\n" + array_types_decls;
}

return to_include + head + array_types_decls + forward_decl_functions + unit_src +
ds_funcs_defined + util_funcs_defined;
}
void visit_TranslationUnit(const ASR::TranslationUnit_t &x) {
global_scope = x.m_global_scope;
// All loose statements must be converted to a function, so the items
Expand Down Expand Up @@ -565,7 +617,7 @@ R"(#include <stdio.h>
}
func += ")";
bracket_open--;
if (f_type->m_abi == ASR::abiType::Source) {
if (is_c && f_type->m_abi == ASR::abiType::Source) {
forward_decl_functions += func + ";\n";
}
if( is_c || template_for_Kokkos.empty() ) {
Expand Down
26 changes: 18 additions & 8 deletions src/libasr/codegen/asr_to_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,13 @@ class ASRToCPPVisitor : public BaseCCPPVisitor<ASRToCPPVisitor>
{
public:

std::string array_types_decls;
std::map<std::string, std::map<std::string,
std::map<size_t, std::string>>> eltypedims2arraytype;

ASRToCPPVisitor(diag::Diagnostics &diag, CompilerOptions &co,
int64_t default_lower_bound)
: BaseCCPPVisitor(diag, co.platform, co, true, true, false,
default_lower_bound),
array_types_decls(std::string("\nstruct dimension_descriptor\n"
"{\n int32_t lower_bound, length;\n};\n")) {}
default_lower_bound) {}

std::string convert_dims(size_t n_dims, ASR::dimension_t *m_dims, size_t& size)
{
Expand Down Expand Up @@ -303,6 +300,11 @@ class ASRToCPPVisitor : public BaseCCPPVisitor<ASRToCPPVisitor>
std::string encoded_type_name = "x" + der_type_name;
std::string type_name = std::string("struct ") + der_type_name;
handle_array(v.m_type, "struct", false)
} else if (ASR::is_a<ASR::List_t>(*v.m_type)) {
ASR::List_t* t = ASR::down_cast<ASR::List_t>(v_m_type);
std::string list_type_c = c_ds_api->get_list_type(t);
sub = format_type_c("", list_type_c, v.m_name,
false, false);
} else {
diag.codegen_error_label("Type number '"
+ std::to_string(v.m_type->type)
Expand All @@ -327,11 +329,18 @@ class ASRToCPPVisitor : public BaseCCPPVisitor<ASRToCPPVisitor>
// All loose statements must be converted to a function, so the items
// must be empty:
LCOMPILERS_ASSERT(x.n_items == 0);
std::string unit_src = "";
indentation_level = 0;
indentation_spaces = 4;

std::string headers =
SymbolTable* current_scope_copy = current_scope;
current_scope = global_scope;
c_ds_api->set_indentation(indentation_level, indentation_spaces);
c_ds_api->set_global_scope(global_scope);
c_utils_functions->set_indentation(indentation_level, indentation_spaces);
c_utils_functions->set_global_scope(global_scope);
c_ds_api->set_c_utils_functions(c_utils_functions.get());

std::string head =
R"(#include <iostream>
#include <string>
#include <vector>
Expand All @@ -356,7 +365,7 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)

// Pre-declare all functions first, then generate code
// Otherwise some function might not be found.
unit_src += "// Forward declarations\n";
std::string unit_src = "// Forward declarations\n";
unit_src += declare_all_functions(*x.m_global_scope);
// Now pre-declare all functions from modules and programs
for (auto &item : x.m_global_scope->get_scope()) {
Expand Down Expand Up @@ -417,7 +426,8 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
}
}

src = headers + array_types_decls + unit_src;
src = get_final_combined_src(head, unit_src);
current_scope = current_scope_copy;
}

void visit_Program(const ASR::Program_t &x) {
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/c-expr_12-93c7780.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "c-expr_12-93c7780.stdout",
"stdout_hash": "4ff750b49402a8f60ae61abdc16289388dfb3230995dda8b75fcd8cd",
"stdout_hash": "6162cc3c452b530aef96db7443c95a0db16bce78b119b9a4ccf91f6a",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
1 change: 1 addition & 0 deletions tests/reference/c-expr_12-93c7780.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string.h>
#include <lfortran_intrinsics.h>


struct dimension_descriptor
{
int32_t lower_bound, length;
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/cpp-assert1-ba60925.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "cpp-assert1-ba60925.stdout",
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
"stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
5 changes: 0 additions & 5 deletions tests/reference/cpp-assert1-ba60925.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
return r;
}


struct dimension_descriptor
{
int32_t lower_bound, length;
};
// Forward declarations
namespace {
}
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/cpp-expr12-fd2ea87.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "cpp-expr12-fd2ea87.stdout",
"stdout_hash": "aa612843d76d425d683903976105593cdfed30ae247de18e7b88e32c",
"stdout_hash": "ade4edd1e3586b786d3860b244053fe7c2f78e518b4392fe95626727",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
5 changes: 0 additions & 5 deletions tests/reference/cpp-expr12-fd2ea87.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
return r;
}


struct dimension_descriptor
{
int32_t lower_bound, length;
};
// Forward declarations
void __main____global_statements();
int32_t check();
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/cpp-expr15-1661c0d.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "cpp-expr15-1661c0d.stdout",
"stdout_hash": "fd8cc5d59bdc0e053f8d5bc81aa076bcd34b0a95e03b985803057995",
"stdout_hash": "9f8482285dd48f735d4d59d18a878e7fca978188cef61719e9a2cad3",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
5 changes: 0 additions & 5 deletions tests/reference/cpp-expr15-1661c0d.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
return r;
}


struct dimension_descriptor
{
int32_t lower_bound, length;
};
// Forward declarations
void __main____global_statements();
double test1();
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/cpp-expr2-09c05ad.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "cpp-expr2-09c05ad.stdout",
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
"stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
5 changes: 0 additions & 5 deletions tests/reference/cpp-expr2-09c05ad.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
return r;
}


struct dimension_descriptor
{
int32_t lower_bound, length;
};
// Forward declarations
namespace {
}
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/cpp-expr5-1de0e30.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "cpp-expr5-1de0e30.stdout",
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
"stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
5 changes: 0 additions & 5 deletions tests/reference/cpp-expr5-1de0e30.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
return r;
}


struct dimension_descriptor
{
int32_t lower_bound, length;
};
// Forward declarations
namespace {
}
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/cpp-expr6-f337f4f.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "cpp-expr6-f337f4f.stdout",
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
"stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
5 changes: 0 additions & 5 deletions tests/reference/cpp-expr6-f337f4f.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
return r;
}


struct dimension_descriptor
{
int32_t lower_bound, length;
};
// Forward declarations
namespace {
}
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/cpp-expr7-529bd53.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "cpp-expr7-529bd53.stdout",
"stdout_hash": "e6a69b9bce9fb3c85fd36e37f7fa56debd52c4b8440a7269950efea5",
"stdout_hash": "0ddb624844237f1333b49b580bb956d0f202df4fa834cff3dc8b4e75",
"stderr": "cpp-expr7-529bd53.stderr",
"stderr_hash": "6e9790ac88db1a9ead8f64a91ba8a6605de67167037908a74b77be0c",
"returncode": 0
Expand Down
5 changes: 0 additions & 5 deletions tests/reference/cpp-expr7-529bd53.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
return r;
}


struct dimension_descriptor
{
int32_t lower_bound, length;
};
// Forward declarations
void __main____global_statements();
void main0();
Expand Down
Loading