Skip to content

C: Fixes from LFortran #2289

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 3 commits into from
Aug 22, 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
20 changes: 18 additions & 2 deletions integration_tests/generics_02.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from lpython import TypeVar, InOut
from lpython import TypeVar, InOut, i32

T = TypeVar('T')

Expand All @@ -10,4 +10,20 @@ def swap(x: InOut[T], y: InOut[T]):
print(x)
print(y)

swap(1,2)
def main0():
a: i32 = 5
b: i32 = 10

# Invalid test case for CPython:
# CPython passes values by copy of object reference
# and hence does not support swapping variables
# passed by arguments.
# Therefore do not add asserts to this file

print(a, b)

swap(a, b)

print(a, b)

main0()
36 changes: 25 additions & 11 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1041,15 +1041,21 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
this->visit_expr(*x.m_arg);
}

std::string construct_call_args(size_t n_args, ASR::call_arg_t* m_args) {
std::string construct_call_args(ASR::Function_t* f, size_t n_args, ASR::call_arg_t* m_args) {
bracket_open++;
std::string args = "";
for (size_t i=0; i<n_args; i++) {
self().visit_expr(*m_args[i].m_value);
ASR::ttype_t* type = ASRUtils::expr_type(m_args[i].m_value);
if (ASR::is_a<ASR::Var_t>(*m_args[i].m_value)) {
if( ASRUtils::is_array(type) &&
ASRUtils::is_pointer(type) ) {
if (ASR::is_a<ASR::Var_t>(*m_args[i].m_value) &&
ASR::is_a<ASR::Variable_t>(
*(ASR::down_cast<ASR::Var_t>(m_args[i].m_value)->m_v))) {
ASR::Variable_t* param = ASRUtils::EXPR2VAR(f->m_args[i]);
if( (ASRUtils::is_array(type) &&
ASRUtils::is_pointer(type))
|| (is_c && (param->m_intent == ASRUtils::intent_inout
|| param->m_intent == ASRUtils::intent_out)
&& !ASRUtils::is_aggregate_type(param->m_type))) {
args += "&" + src;
} else {
args += src;
Expand Down Expand Up @@ -1107,7 +1113,7 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
+ "' not implemented");
}
} else {
src = fn_name + "(" + construct_call_args(x.n_args, x.m_args) + ")";
src = fn_name + "(" + construct_call_args(fn, x.n_args, x.m_args) + ")";
}
last_expr_precedence = 2;
if( ASR::is_a<ASR::List_t>(*x.m_type) ) {
Expand Down Expand Up @@ -1762,11 +1768,19 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
return;
}
ASR::Variable_t* sv = ASR::down_cast<ASR::Variable_t>(s);
if( (sv->m_intent == ASRUtils::intent_in ||
sv->m_intent == ASRUtils::intent_inout) &&
is_c && ASRUtils::is_array(sv->m_type) &&
ASRUtils::is_pointer(sv->m_type)) {
src = "(*" + std::string(ASR::down_cast<ASR::Variable_t>(s)->m_name) + ")";
if (is_c) {
if ((sv->m_intent == ASRUtils::intent_in
|| sv->m_intent == ASRUtils::intent_inout)
&& ASRUtils::is_array(sv->m_type)
&& ASRUtils::is_pointer(sv->m_type)) {
src = "(*" + std::string(ASR::down_cast<ASR::Variable_t>(s)->m_name) + ")";
} else if ((sv->m_intent == ASRUtils::intent_inout
|| sv->m_intent == ASRUtils::intent_out)
&& !ASRUtils::is_aggregate_type(sv->m_type)) {
src = "(*" + std::string(ASR::down_cast<ASR::Variable_t>(s)->m_name) + ")";
} else {
src = std::string(ASR::down_cast<ASR::Variable_t>(s)->m_name);
}
} else {
src = std::string(ASR::down_cast<ASR::Variable_t>(s)->m_name);
}
Expand Down Expand Up @@ -2736,7 +2750,7 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
if (sym_name == "main") {
sym_name = "_xx_lcompilers_changed_main_xx";
}
src = indent + sym_name + "(" + construct_call_args(x.n_args, x.m_args) + ");\n";
src = indent + sym_name + "(" + construct_call_args(s, x.n_args, x.m_args) + ");\n";
}

#define SET_INTRINSIC_NAME(X, func_name) \
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/asr-generics_02-e2ea5c9.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-generics_02-e2ea5c9",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/generics_02.py",
"infile_hash": "f34adf5280ade332f6f72c81cdc0d18b463176f23df500f7a08370e0",
"infile_hash": "8406b1b9329997678ed067f896373fbbf49daeb2e379c700c5c2e917",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-generics_02-e2ea5c9.stdout",
"stdout_hash": "06fea11e3d245974eb6e026ef7754acae43816a4cbd10c7c27ce37c8",
"stdout_hash": "f5a159e575ecf0fdad20418b4106f3da761a8abaf39ed65de2a7b83e",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
133 changes: 112 additions & 21 deletions tests/reference/asr-generics_02-e2ea5c9.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
__asr_generic_swap_0:
(Function
(SymbolTable
4
5
{
temp:
(Variable
4
5
temp
[]
Local
Expand All @@ -48,7 +48,7 @@
),
x:
(Variable
4
5
x
[]
InOut
Expand All @@ -64,7 +64,7 @@
),
y:
(Variable
4
5
y
[]
InOut
Expand Down Expand Up @@ -96,32 +96,32 @@
.false.
)
[]
[(Var 4 x)
(Var 4 y)]
[(Var 5 x)
(Var 5 y)]
[(=
(Var 4 temp)
(Var 4 x)
(Var 5 temp)
(Var 5 x)
()
)
(=
(Var 4 x)
(Var 4 y)
(Var 5 x)
(Var 5 y)
()
)
(=
(Var 4 y)
(Var 4 temp)
(Var 5 y)
(Var 5 temp)
()
)
(Print
()
[(Var 4 x)]
[(Var 5 x)]
()
()
)
(Print
()
[(Var 4 y)]
[(Var 5 y)]
()
()
)]
Expand All @@ -134,7 +134,7 @@
__main__global_stmts:
(Function
(SymbolTable
5
6
{

})
Expand All @@ -153,13 +153,104 @@
[]
.false.
)
[__asr_generic_swap_0]
[main0]
[]
[(SubroutineCall
2 main0
()
[]
()
)]
()
Public
.false.
.false.
()
),
main0:
(Function
(SymbolTable
4
{
a:
(Variable
4
a
[]
Local
()
()
Default
(Integer 4)
()
Source
Public
Required
.false.
),
b:
(Variable
4
b
[]
Local
()
()
Default
(Integer 4)
()
Source
Public
Required
.false.
)
})
main0
(FunctionType
[]
()
Source
Implementation
()
.false.
.false.
.false.
.false.
.false.
[]
.false.
)
[__asr_generic_swap_0]
[]
[(=
(Var 4 a)
(IntegerConstant 5 (Integer 4))
()
)
(=
(Var 4 b)
(IntegerConstant 10 (Integer 4))
()
)
(Print
()
[(Var 4 a)
(Var 4 b)]
()
()
)
(SubroutineCall
2 __asr_generic_swap_0
()
[((IntegerConstant 1 (Integer 4)))
((IntegerConstant 2 (Integer 4)))]
[((Var 4 a))
((Var 4 b))]
()
)
(Print
()
[(Var 4 a)
(Var 4 b)]
()
()
)]
()
Expand Down Expand Up @@ -293,11 +384,11 @@
main_program:
(Program
(SymbolTable
6
7
{
__main__global_stmts:
(ExternalSymbol
6
7
__main__global_stmts
2 __main__global_stmts
__main__
Expand All @@ -309,7 +400,7 @@
main_program
[__main__]
[(SubroutineCall
6 __main__global_stmts
7 __main__global_stmts
2 __main__global_stmts
[]
()
Expand Down