Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22760ba

Browse files
committed
Added support for C backend
1 parent 80657e7 commit 22760ba

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ RUN(NAME structs_13 LABELS llvm c
335335
EXTRAFILES structs_13b.c)
336336
RUN(NAME structs_14 LABELS cpython llvm c)
337337
RUN(NAME structs_15 LABELS cpython llvm c)
338+
RUN(NAME structs_16 LABELS cpython llvm c)
338339
RUN(NAME sizeof_01 LABELS llvm c
339340
EXTRAFILES sizeof_01b.c)
340341
RUN(NAME enum_01 LABELS cpython llvm c)

src/libasr/asr_verify.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ class VerifyVisitor : public BaseWalkVisitor<VerifyVisitor>
348348
ASR::symbol_t* sym = ASR::down_cast<ASR::Union_t>(var_type)->m_union_type;
349349
aggregate_type_name = ASRUtils::symbol_name(sym);
350350
}
351-
if( aggregate_type_name ) {
351+
if( aggregate_type_name &&
352+
!current_symtab->get_symbol(std::string(aggregate_type_name)) ) {
352353
struct_dependencies.push_back(std::string(aggregate_type_name));
353354
require(present(x.m_dependencies, x.n_dependencies, std::string(aggregate_type_name)),
354355
std::string(x.m_name) + " depends on " + std::string(aggregate_type_name)

src/libasr/codegen/asr_to_c.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
139139
void allocate_array_members_of_struct(ASR::StructType_t* der_type_t, std::string& sub,
140140
std::string indent, std::string name) {
141141
for( auto itr: der_type_t->m_symtab->get_scope() ) {
142+
if( ASR::is_a<ASR::UnionType_t>(*itr.second) ) {
143+
continue ;
144+
}
142145
ASR::ttype_t* mem_type = ASRUtils::symbol_type(itr.second);
143146
if( ASRUtils::is_character(*mem_type) ) {
144147
sub += indent + name + "->" + itr.first + " = (char*) malloc(40 * sizeof(char));\n";
@@ -394,7 +397,8 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
394397
} else if (ASR::is_a<ASR::Union_t>(*v_m_type)) {
395398
std::string indent(indentation_level*indentation_spaces, ' ');
396399
ASR::Union_t *t = ASR::down_cast<ASR::Union_t>(v_m_type);
397-
std::string der_type_name = ASRUtils::symbol_name(t->m_union_type);
400+
std::string der_type_name = ASRUtils::symbol_name(
401+
ASRUtils::symbol_get_past_external(t->m_union_type));
398402
if( is_array ) {
399403
bool is_fixed_size = true;
400404
dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size, true);
@@ -682,11 +686,20 @@ R"(
682686
}
683687

684688
template <typename T>
685-
void visit_AggregateTypeUtil(const T& x, std::string c_type_name) {
689+
void visit_AggregateTypeUtil(const T& x, std::string c_type_name,
690+
std::string& src_dest) {
691+
std::string body = "";
692+
int indendation_level_copy = indentation_level;
693+
for( auto itr: x.m_symtab->get_scope() ) {
694+
if(ASR::is_a<ASR::UnionType_t>(*itr.second)) {
695+
visit_AggregateTypeUtil(*ASR::down_cast<ASR::UnionType_t>(itr.second),
696+
"union", src_dest);
697+
}
698+
}
699+
indentation_level = indendation_level_copy;
686700
std::string indent(indentation_level*indentation_spaces, ' ');
687701
indentation_level += 1;
688702
std::string open_struct = indent + c_type_name + " " + std::string(x.m_name) + " {\n";
689-
std::string body = "";
690703
indent.push_back(' ');
691704
for( size_t i = 0; i < x.n_members; i++ ) {
692705
ASR::symbol_t* member = x.m_symtab->get_symbol(x.m_members[i]);
@@ -701,7 +714,7 @@ R"(
701714
}
702715
indentation_level -= 1;
703716
std::string end_struct = "};\n\n";
704-
array_types_decls += open_struct + body + end_struct;
717+
src_dest += open_struct + body + end_struct;
705718
}
706719

707720
void visit_StructType(const ASR::StructType_t& x) {
@@ -721,12 +734,12 @@ R"(
721734
attr_args += ")";
722735
c_type_name += " __attribute__(" + attr_args + ")";
723736
}
724-
visit_AggregateTypeUtil(x, c_type_name);
737+
visit_AggregateTypeUtil(x, c_type_name, array_types_decls);
725738
src = "";
726739
}
727740

728741
void visit_UnionType(const ASR::UnionType_t& x) {
729-
visit_AggregateTypeUtil(x, "union");
742+
visit_AggregateTypeUtil(x, "union", array_types_decls);
730743
}
731744

732745
void visit_EnumType(const ASR::EnumType_t& x) {

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
24142414
aggregate_type_name = ASRUtils::symbol_name(
24152415
ASR::down_cast<ASR::Union_t>(var_type)->m_union_type);
24162416
}
2417-
if( aggregate_type_name ) {
2417+
if( aggregate_type_name &&
2418+
!current_scope->get_symbol(std::string(aggregate_type_name)) ) {
24182419
struct_dependencies.push_back(al, aggregate_type_name);
24192420
}
24202421
member_names.push_back(al, n->m_id);

0 commit comments

Comments
 (0)