From c547f0e080e1468e99951fa6c6008d846d1a8fdd Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Tue, 21 Jun 2022 20:18:52 +0530 Subject: [PATCH 1/3] Added subroutine call for comparison --- integration_tests/expr_10.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/integration_tests/expr_10.py b/integration_tests/expr_10.py index d932675749..24da1000ee 100644 --- a/integration_tests/expr_10.py +++ b/integration_tests/expr_10.py @@ -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() From 90f7f825090335e5a1afeb94d2cb703f03c11779 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Tue, 21 Jun 2022 20:19:16 +0530 Subject: [PATCH 2/3] Ignore return value by assigning result to a dummy variable --- src/lpython/semantics/python_ast_to_asr.cpp | 24 +++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index a0a159fd22..79799d78a3 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -667,7 +667,8 @@ class CommonVisitor : public AST::BaseVisitor { // 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 args, std::string call_name, const Location &loc) { + Vec 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(*s); @@ -725,8 +726,23 @@ class CommonVisitor : public AST::BaseVisitor { Vec 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(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(*s)) { ASR::Subroutine_t *func = ASR::down_cast(s); if (args.size() != func->n_args) { @@ -3284,7 +3300,7 @@ class BodyVisitor : public CommonVisitor { 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); From 9422079562f14196e6561290adf473f2917bacad Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Tue, 21 Jun 2022 20:19:31 +0530 Subject: [PATCH 3/3] Updated reference tests --- integration_tests/CMakeLists.txt | 2 +- tests/reference/asr-expr_10-d39708c.json | 13 +++++++++++++ tests/reference/asr-expr_10-d39708c.stdout | 1 + tests/tests.toml | 4 ++++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/reference/asr-expr_10-d39708c.json create mode 100644 tests/reference/asr-expr_10-d39708c.stdout diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index e96f447a62..f948df019a 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -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) diff --git a/tests/reference/asr-expr_10-d39708c.json b/tests/reference/asr-expr_10-d39708c.json new file mode 100644 index 0000000000..957be9c4b1 --- /dev/null +++ b/tests/reference/asr-expr_10-d39708c.json @@ -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 +} \ No newline at end of file diff --git a/tests/reference/asr-expr_10-d39708c.stdout b/tests/reference/asr-expr_10-d39708c.stdout new file mode 100644 index 0000000000..2a647b8c80 --- /dev/null +++ b/tests/reference/asr-expr_10-d39708c.stdout @@ -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.)}) []) diff --git a/tests/tests.toml b/tests/tests.toml index 6d3117fa89..89d6a7851d 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -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