Skip to content

Commit b3add2a

Browse files
authored
Merge pull request #2228 from Shaikh-Ubaid/cpp_fix_issue_2221
CPP: Fix list repeat issue 2221
2 parents 0580c44 + 9860c3e commit b3add2a

40 files changed

+365
-155
lines changed

src/libasr/codegen/asr_to_c.cpp

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
3030
{
3131
public:
3232

33-
std::unique_ptr<CUtils::CUtilFunctions> c_utils_functions;
34-
3533
int counter;
3634

3735
ASRToCVisitor(diag::Diagnostics &diag, CompilerOptions &co,
3836
int64_t default_lower_bound)
3937
: BaseCCPPVisitor(diag, co.platform, co, false, false, true, default_lower_bound),
40-
c_utils_functions{std::make_unique<CUtils::CUtilFunctions>()},
4138
counter{0} {
4239
}
4340

@@ -602,12 +599,6 @@ R"(
602599

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

612603
std::string unit_src_tmp;
613604
for (auto &item : x.m_global_scope->get_scope()) {
@@ -700,48 +691,10 @@ R"(
700691
unit_src += src;
701692
}
702693
}
703-
std::string to_include = "";
704-
for (auto &s: user_defines) {
705-
to_include += "#define " + s + "\n";
706-
}
707-
for (auto &s: headers) {
708-
to_include += "#include <" + s + ">\n";
709-
}
710-
for (auto &s: user_headers) {
711-
to_include += "#include \"" + s + "\"\n";
712-
}
713-
if( c_ds_api->get_func_decls().size() > 0 ) {
714-
array_types_decls += "\n" + c_ds_api->get_func_decls() + "\n";
715-
}
716-
if( c_utils_functions->get_util_func_decls().size() > 0 ) {
717-
array_types_decls += "\n" + c_utils_functions->get_util_func_decls() + "\n";
718-
}
719-
std::string ds_funcs_defined = "";
720-
if( c_ds_api->get_generated_code().size() > 0 ) {
721-
ds_funcs_defined = "\n" + c_ds_api->get_generated_code() + "\n";
722-
}
723-
std::string util_funcs_defined = "";
724-
if( c_utils_functions->get_generated_code().size() > 0 ) {
725-
util_funcs_defined = "\n" + c_utils_functions->get_generated_code() + "\n";
726-
}
727-
if( bind_py_utils_functions->get_util_func_decls().size() > 0 ) {
728-
array_types_decls += "\n" + bind_py_utils_functions->get_util_func_decls() + "\n";
729-
}
730-
if( bind_py_utils_functions->get_generated_code().size() > 0 ) {
731-
util_funcs_defined = "\n" + bind_py_utils_functions->get_generated_code() + "\n";
732-
}
733-
if( is_string_concat_present ) {
734-
head += strcat_def;
735-
}
736694

737-
// Include dimension_descriptor definition that is used by array types
738-
if (array_types_decls.size() != 0) {
739-
array_types_decls.insert(0, "struct dimension_descriptor\n"
740-
"{\n int32_t lower_bound, length;\n};\n");
741-
}
742695
forward_decl_functions += "\n\n";
743-
src = to_include + head + array_types_decls + forward_decl_functions + unit_src +
744-
ds_funcs_defined + util_funcs_defined;
696+
src = get_final_combined_src(head, unit_src);
697+
745698
if (!emit_headers.empty()) {
746699
std::string to_includes_1 = "";
747700
for (auto &s: headers) {

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
158158
std::string from_std_vector_helper;
159159

160160
std::unique_ptr<CCPPDSUtils> c_ds_api;
161+
std::unique_ptr<CUtils::CUtilFunctions> c_utils_functions;
161162
std::unique_ptr<BindPyUtils::BindPyUtilFunctions> bind_py_utils_functions;
162163
std::string const_name;
163164
size_t const_vars_count;
@@ -185,12 +186,63 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
185186
gen_stdstring{gen_stdstring}, gen_stdcomplex{gen_stdcomplex},
186187
is_c{is_c}, global_scope{nullptr}, lower_bound{default_lower_bound},
187188
template_number{0}, c_ds_api{std::make_unique<CCPPDSUtils>(is_c, platform)},
189+
c_utils_functions{std::make_unique<CUtils::CUtilFunctions>()},
188190
bind_py_utils_functions{std::make_unique<BindPyUtils::BindPyUtilFunctions>()},
189191
const_name{"constname"},
190192
const_vars_count{0}, loop_end_count{0}, bracket_open{0},
191193
is_string_concat_present{false} {
192194
}
193195

196+
std::string get_final_combined_src(std::string head, std::string unit_src) {
197+
std::string to_include = "";
198+
for (auto &s: user_defines) {
199+
to_include += "#define " + s + "\n";
200+
}
201+
for (auto &s: headers) {
202+
to_include += "#include <" + s + ">\n";
203+
}
204+
for (auto &s: user_headers) {
205+
to_include += "#include \"" + s + "\"\n";
206+
}
207+
if( c_ds_api->get_func_decls().size() > 0 ) {
208+
array_types_decls += "\n" + c_ds_api->get_func_decls() + "\n";
209+
}
210+
if( c_utils_functions->get_util_func_decls().size() > 0 ) {
211+
array_types_decls += "\n" + c_utils_functions->get_util_func_decls() + "\n";
212+
}
213+
std::string ds_funcs_defined = "";
214+
if( c_ds_api->get_generated_code().size() > 0 ) {
215+
ds_funcs_defined = "\n" + c_ds_api->get_generated_code() + "\n";
216+
}
217+
std::string util_funcs_defined = "";
218+
if( c_utils_functions->get_generated_code().size() > 0 ) {
219+
util_funcs_defined = "\n" + c_utils_functions->get_generated_code() + "\n";
220+
}
221+
if( bind_py_utils_functions->get_util_func_decls().size() > 0 ) {
222+
array_types_decls += "\n" + bind_py_utils_functions->get_util_func_decls() + "\n";
223+
}
224+
if( bind_py_utils_functions->get_generated_code().size() > 0 ) {
225+
util_funcs_defined = "\n" + bind_py_utils_functions->get_generated_code() + "\n";
226+
}
227+
if( is_string_concat_present ) {
228+
std::string strcat_def = "";
229+
strcat_def += " char* " + global_scope->get_unique_name("strcat_", false) + "(char* x, char* y) {\n";
230+
strcat_def += " char* str_tmp = (char*) malloc((strlen(x) + strlen(y) + 2) * sizeof(char));\n";
231+
strcat_def += " strcpy(str_tmp, x);\n";
232+
strcat_def += " return strcat(str_tmp, y);\n";
233+
strcat_def += " }\n\n";
234+
head += strcat_def;
235+
}
236+
237+
// Include dimension_descriptor definition that is used by array types
238+
if (array_types_decls.size() != 0) {
239+
array_types_decls = "\nstruct dimension_descriptor\n"
240+
"{\n int32_t lower_bound, length;\n};\n" + array_types_decls;
241+
}
242+
243+
return to_include + head + array_types_decls + forward_decl_functions + unit_src +
244+
ds_funcs_defined + util_funcs_defined;
245+
}
194246
void visit_TranslationUnit(const ASR::TranslationUnit_t &x) {
195247
global_scope = x.m_global_scope;
196248
// All loose statements must be converted to a function, so the items
@@ -565,7 +617,7 @@ R"(#include <stdio.h>
565617
}
566618
func += ")";
567619
bracket_open--;
568-
if (f_type->m_abi == ASR::abiType::Source) {
620+
if (is_c && f_type->m_abi == ASR::abiType::Source) {
569621
forward_decl_functions += func + ";\n";
570622
}
571623
if( is_c || template_for_Kokkos.empty() ) {

src/libasr/codegen/asr_to_cpp.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,13 @@ class ASRToCPPVisitor : public BaseCCPPVisitor<ASRToCPPVisitor>
7373
{
7474
public:
7575

76-
std::string array_types_decls;
7776
std::map<std::string, std::map<std::string,
7877
std::map<size_t, std::string>>> eltypedims2arraytype;
7978

8079
ASRToCPPVisitor(diag::Diagnostics &diag, CompilerOptions &co,
8180
int64_t default_lower_bound)
8281
: BaseCCPPVisitor(diag, co.platform, co, true, true, false,
83-
default_lower_bound),
84-
array_types_decls(std::string("\nstruct dimension_descriptor\n"
85-
"{\n int32_t lower_bound, length;\n};\n")) {}
82+
default_lower_bound) {}
8683

8784
std::string convert_dims(size_t n_dims, ASR::dimension_t *m_dims, size_t& size)
8885
{
@@ -303,6 +300,11 @@ class ASRToCPPVisitor : public BaseCCPPVisitor<ASRToCPPVisitor>
303300
std::string encoded_type_name = "x" + der_type_name;
304301
std::string type_name = std::string("struct ") + der_type_name;
305302
handle_array(v.m_type, "struct", false)
303+
} else if (ASR::is_a<ASR::List_t>(*v.m_type)) {
304+
ASR::List_t* t = ASR::down_cast<ASR::List_t>(v_m_type);
305+
std::string list_type_c = c_ds_api->get_list_type(t);
306+
sub = format_type_c("", list_type_c, v.m_name,
307+
false, false);
306308
} else {
307309
diag.codegen_error_label("Type number '"
308310
+ std::to_string(v.m_type->type)
@@ -327,11 +329,18 @@ class ASRToCPPVisitor : public BaseCCPPVisitor<ASRToCPPVisitor>
327329
// All loose statements must be converted to a function, so the items
328330
// must be empty:
329331
LCOMPILERS_ASSERT(x.n_items == 0);
330-
std::string unit_src = "";
331332
indentation_level = 0;
332333
indentation_spaces = 4;
333334

334-
std::string headers =
335+
SymbolTable* current_scope_copy = current_scope;
336+
current_scope = global_scope;
337+
c_ds_api->set_indentation(indentation_level, indentation_spaces);
338+
c_ds_api->set_global_scope(global_scope);
339+
c_utils_functions->set_indentation(indentation_level, indentation_spaces);
340+
c_utils_functions->set_global_scope(global_scope);
341+
c_ds_api->set_c_utils_functions(c_utils_functions.get());
342+
343+
std::string head =
335344
R"(#include <iostream>
336345
#include <string>
337346
#include <vector>
@@ -356,7 +365,7 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
356365

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

420-
src = headers + array_types_decls + unit_src;
429+
src = get_final_combined_src(head, unit_src);
430+
current_scope = current_scope_copy;
421431
}
422432

423433
void visit_Program(const ASR::Program_t &x) {

tests/reference/c-expr_12-93c7780.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "c-expr_12-93c7780.stdout",
9-
"stdout_hash": "4ff750b49402a8f60ae61abdc16289388dfb3230995dda8b75fcd8cd",
9+
"stdout_hash": "6162cc3c452b530aef96db7443c95a0db16bce78b119b9a4ccf91f6a",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/c-expr_12-93c7780.stdout

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string.h>
77
#include <lfortran_intrinsics.h>
88

9+
910
struct dimension_descriptor
1011
{
1112
int32_t lower_bound, length;

tests/reference/cpp-assert1-ba60925.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-assert1-ba60925.stdout",
9-
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
9+
"stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-assert1-ba60925.stdout

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
1717
return r;
1818
}
1919

20-
21-
struct dimension_descriptor
22-
{
23-
int32_t lower_bound, length;
24-
};
2520
// Forward declarations
2621
namespace {
2722
}

tests/reference/cpp-expr12-fd2ea87.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr12-fd2ea87.stdout",
9-
"stdout_hash": "aa612843d76d425d683903976105593cdfed30ae247de18e7b88e32c",
9+
"stdout_hash": "ade4edd1e3586b786d3860b244053fe7c2f78e518b4392fe95626727",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr12-fd2ea87.stdout

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
1717
return r;
1818
}
1919

20-
21-
struct dimension_descriptor
22-
{
23-
int32_t lower_bound, length;
24-
};
2520
// Forward declarations
2621
void __main____global_statements();
2722
int32_t check();

tests/reference/cpp-expr15-1661c0d.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr15-1661c0d.stdout",
9-
"stdout_hash": "fd8cc5d59bdc0e053f8d5bc81aa076bcd34b0a95e03b985803057995",
9+
"stdout_hash": "9f8482285dd48f735d4d59d18a878e7fca978188cef61719e9a2cad3",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr15-1661c0d.stdout

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
1717
return r;
1818
}
1919

20-
21-
struct dimension_descriptor
22-
{
23-
int32_t lower_bound, length;
24-
};
2520
// Forward declarations
2621
void __main____global_statements();
2722
double test1();

tests/reference/cpp-expr2-09c05ad.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr2-09c05ad.stdout",
9-
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
9+
"stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr2-09c05ad.stdout

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
1717
return r;
1818
}
1919

20-
21-
struct dimension_descriptor
22-
{
23-
int32_t lower_bound, length;
24-
};
2520
// Forward declarations
2621
namespace {
2722
}

tests/reference/cpp-expr5-1de0e30.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr5-1de0e30.stdout",
9-
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
9+
"stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr5-1de0e30.stdout

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
1717
return r;
1818
}
1919

20-
21-
struct dimension_descriptor
22-
{
23-
int32_t lower_bound, length;
24-
};
2520
// Forward declarations
2621
namespace {
2722
}

tests/reference/cpp-expr6-f337f4f.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr6-f337f4f.stdout",
9-
"stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2",
9+
"stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr6-f337f4f.stdout

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
1717
return r;
1818
}
1919

20-
21-
struct dimension_descriptor
22-
{
23-
int32_t lower_bound, length;
24-
};
2520
// Forward declarations
2621
namespace {
2722
}

tests/reference/cpp-expr7-529bd53.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr7-529bd53.stdout",
9-
"stdout_hash": "e6a69b9bce9fb3c85fd36e37f7fa56debd52c4b8440a7269950efea5",
9+
"stdout_hash": "0ddb624844237f1333b49b580bb956d0f202df4fa834cff3dc8b4e75",
1010
"stderr": "cpp-expr7-529bd53.stderr",
1111
"stderr_hash": "6e9790ac88db1a9ead8f64a91ba8a6605de67167037908a74b77be0c",
1212
"returncode": 0

tests/reference/cpp-expr7-529bd53.stdout

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
1717
return r;
1818
}
1919

20-
21-
struct dimension_descriptor
22-
{
23-
int32_t lower_bound, length;
24-
};
2520
// Forward declarations
2621
void __main____global_statements();
2722
void main0();

0 commit comments

Comments
 (0)