Skip to content

Commit

Permalink
Merge pull request lcompilers#668 from czgdp1807/ignore_ret
Browse files Browse the repository at this point in the history
Ignore return types by assigning to a dummy variable
  • Loading branch information
certik committed Jun 21, 2022
2 parents f49908e + 9422079 commit 693bc8d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ RUN(NAME expr_06 LABELS cpython llvm)
RUN(NAME expr_07 LABELS cpython llvm)
RUN(NAME expr_08 LABELS llvm c)
RUN(NAME expr_09 LABELS cpython llvm)
RUN(NAME expr_10 LABELS cpython)
RUN(NAME expr_10 LABELS cpython llvm)
RUN(NAME test_types_01 LABELS cpython llvm)
RUN(NAME test_str_01 LABELS cpython llvm)
RUN(NAME test_str_02 LABELS cpython llvm)
Expand Down
6 changes: 6 additions & 0 deletions integration_tests/expr_10.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
def g() -> i32:
return 5

def gsubrout(x: i32):
print(x)

def test_fn1():
i: i32 = g()
j: i32
j = g()
g()
gsubrout(i)

test_fn1()
24 changes: 20 additions & 4 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,8 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
// Function to create appropriate call based on symbol type. If it is external
// generic symbol then it changes the name accordingly.
ASR::asr_t* make_call_helper(Allocator &al, ASR::symbol_t* s, SymbolTable *current_scope,
Vec<ASR::call_arg_t> args, std::string call_name, const Location &loc) {
Vec<ASR::call_arg_t> args, std::string call_name, const Location &loc,
bool ignore_return_value=false) {
ASR::symbol_t *s_generic = nullptr, *stemp = s;
// handling ExternalSymbol
bool is_external = ASR::is_a<ASR::ExternalSymbol_t>(*s);
Expand Down Expand Up @@ -725,8 +726,23 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
Vec<ASR::call_arg_t> args_new;
args_new.reserve(al, func->n_args);
visit_expr_list_with_cast(func->m_args, func->n_args, args_new, args);
return ASR::make_FunctionCall_t(al, loc, stemp,
s_generic, args_new.p, args_new.size(), a_type, value, nullptr);
ASR::asr_t* func_call_asr = ASR::make_FunctionCall_t(al, loc, stemp,
s_generic, args_new.p, args_new.size(),
a_type, value, nullptr);
if( ignore_return_value ) {
std::string dummy_ret_name = current_scope->get_unique_name("__lpython_dummy");
ASR::asr_t* variable_asr = ASR::make_Variable_t(al, loc, current_scope,
s2c(al, dummy_ret_name), ASR::intentType::Local,
nullptr, nullptr, ASR::storage_typeType::Default,
a_type, ASR::abiType::Source, ASR::accessType::Public,
ASR::presenceType::Required, false);
ASR::symbol_t* variable_sym = ASR::down_cast<ASR::symbol_t>(variable_asr);
current_scope->add_symbol(dummy_ret_name, variable_sym);
ASR::expr_t* variable_var = ASRUtils::EXPR(ASR::make_Var_t(al, loc, variable_sym));
return ASR::make_Assignment_t(al, loc, variable_var, ASRUtils::EXPR(func_call_asr), nullptr);
} else {
return func_call_asr;
}
} else if (ASR::is_a<ASR::Subroutine_t>(*s)) {
ASR::Subroutine_t *func = ASR::down_cast<ASR::Subroutine_t>(s);
if (args.size() != func->n_args) {
Expand Down Expand Up @@ -3284,7 +3300,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
x.base.base.loc);
}
tmp = make_call_helper(al, s, current_scope, args, call_name,
x.base.base.loc);
x.base.base.loc, true);
return;
}
this->visit_expr(*x.m_value);
Expand Down
13 changes: 13 additions & 0 deletions tests/reference/asr-expr_10-d39708c.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-expr_10-d39708c",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/expr_10.py",
"infile_hash": "00aac96059a6e3d6615e357e73cd79b96646d200ef89978251c8ef5a",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-expr_10-d39708c.stdout",
"stdout_hash": "67e5a6431aa7fea394fe49d7e034c580a88bb71aa6806c199fb3d14b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
1 change: 1 addition & 0 deletions tests/reference/asr-expr_10-d39708c.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Subroutine (SymbolTable 6 {}) _lpython_main_program [] [(SubroutineCall 1 test_fn1 () [] ())] Source Public Implementation () .false. .false.), g: (Function (SymbolTable 2 {_lpython_return_variable: (Variable 2 _lpython_return_variable ReturnVar () () Default (Integer 4 []) Source Public Required .false.)}) g [] [(= (Var 2 _lpython_return_variable) (IntegerConstant 5 (Integer 4 [])) ()) (Return)] (Var 2 _lpython_return_variable) Source Public Implementation ()), gsubrout: (Subroutine (SymbolTable 3 {x: (Variable 3 x In () () Default (Integer 4 []) Source Public Required .false.)}) gsubrout [(Var 3 x)] [(Print () [(Var 3 x)])] Source Public Implementation () .false. .false.), main_program: (Program (SymbolTable 5 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())]), test_fn1: (Subroutine (SymbolTable 4 {__lpython_dummy: (Variable 4 __lpython_dummy Local () () Default (Integer 4 []) Source Public Required .false.), i: (Variable 4 i Local () () Default (Integer 4 []) Source Public Required .false.), j: (Variable 4 j Local () () Default (Integer 4 []) Source Public Required .false.)}) test_fn1 [] [(= (Var 4 i) (FunctionCall 1 g () [] (Integer 4 []) () ()) ()) (= (Var 4 j) (FunctionCall 1 g () [] (Integer 4 []) () ()) ()) (= (Var 4 __lpython_dummy) (FunctionCall 1 g () [] (Integer 4 []) () ()) ()) (SubroutineCall 1 gsubrout () [((Var 4 i))] ())] Source Public Implementation () .false. .false.)}) [])
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ asr = true
filename = "../integration_tests/expr_09.py"
asr = true

[[test]]
filename = "../integration_tests/expr_10.py"
asr = true

[[test]]
filename = "loop1.py"
ast = true
Expand Down

0 comments on commit 693bc8d

Please sign in to comment.