Skip to content

Handle structs as return type in C and LLVM backends #1288

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ RUN(NAME structs_07 LABELS llvm c
RUN(NAME structs_08 LABELS cpython llvm c)
RUN(NAME structs_09 LABELS cpython llvm c)
RUN(NAME structs_10 LABELS cpython llvm c)
RUN(NAME structs_11 LABELS cpython llvm c)
RUN(NAME sizeof_01 LABELS llvm c
EXTRAFILES sizeof_01b.c)
RUN(NAME enum_01 LABELS cpython llvm c)
Expand Down
18 changes: 18 additions & 0 deletions integration_tests/structs_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from ltypes import i32, f64, dataclass

@dataclass
class A:
x: i32
y: f64

def f(x_: i32, y_: f64) -> A:
a_struct: A = A(x_, y_)
return a_struct

def test_struct_return():
b: A = f(0, 1.0)
print(b.x, b.y)
assert b.x == 0
assert b.y == 1.0

test_struct_return()
16 changes: 14 additions & 2 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ R"(#include <stdio.h>
ASR::Pointer_t* ptr_type = ASR::down_cast<ASR::Pointer_t>(return_var->m_type);
std::string pointer_type_str = CUtils::get_c_type_from_ttype_t(ptr_type->m_type);
sub = pointer_type_str + "*";
} else if (ASR::is_a<ASR::Struct_t>(*return_var->m_type)) {
std::string struct_type_str = CUtils::get_c_type_from_ttype_t(return_var->m_type);
sub = struct_type_str + " ";
} else {
throw CodeGenError("Return type not supported in function '" +
std::string(x.m_name) +
Expand Down Expand Up @@ -443,8 +446,15 @@ R"(#include <stdio.h>
for (auto &item : x.m_symtab->get_scope()) {
if (ASR::is_a<ASR::Variable_t>(*item.second)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(item.second);
if (v->m_intent == LFortran::ASRUtils::intent_local || v->m_intent == LFortran::ASRUtils::intent_return_var) {
decl += indent + self().convert_variable_decl(*v);
if (v->m_intent == LFortran::ASRUtils::intent_local ||
v->m_intent == LFortran::ASRUtils::intent_return_var) {
if (is_c) {
decl += indent + self().convert_variable_decl(*v, true,
!(ASR::is_a<ASR::Struct_t>(*v->m_type) &&
std::string(v->m_name) == "_lpython_return_variable"), true);
} else {
decl += indent + self().convert_variable_decl(*v);
}
if( !ASR::is_a<ASR::Const_t>(*v->m_type) ||
v->m_intent == ASRUtils::intent_return_var ) {
decl += ";\n";
Expand Down Expand Up @@ -678,6 +688,8 @@ R"(#include <stdio.h>
value = "*" + value;
} else if (ASR::is_a<ASR::ArrayItem_t>(*x.m_value)) {
value = "&" + value;
} else if (ASR::is_a<ASR::FunctionCall_t>(*x.m_value)) {
target = "*" + target;
}
}
if( !from_std_vector_helper.empty() ) {
Expand Down
5 changes: 4 additions & 1 deletion src/libasr/codegen/asr_to_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,11 @@ class ASRToCPPVisitor : public BaseCCPPVisitor<ASRToCPPVisitor>
}

std::string convert_variable_decl(const ASR::Variable_t &v, bool use_static=true,
bool use_templates_for_arrays=false)
bool use_templates_for_arrays=false, bool _=false)
{
if( _ ) {

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

I recommend not using default arguments, and just use bool /* arg_name */.

std::string sub;
bool use_ref = (v.m_intent == LFortran::ASRUtils::intent_out ||

Expand Down
5 changes: 3 additions & 2 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3346,9 +3346,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
return_type = get_type_from_ttype_t_util(ASRUtils::get_contained_type(return_var_type0))->getPointerTo();
break;
}
case (ASR::ttypeType::Struct) :
throw CodeGenError("Struct return type not implemented yet");
case (ASR::ttypeType::Struct) : {
return_type = get_type_from_ttype_t_util(return_var_type0);
break;
}
case (ASR::ttypeType::Tuple) : {
ASR::Tuple_t* asr_tuple = ASR::down_cast<ASR::Tuple_t>(return_var_type0);
std::string type_code = ASRUtils::get_type_code(asr_tuple->m_type,
Expand Down