Skip to content

Fixing initialisation at declaration issues #642

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 11 commits into from
Jun 20, 2022
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ RUN(NAME expr_02 LABELS cpython llvm c)
RUN(NAME expr_03 LABELS cpython llvm c)
RUN(NAME expr_04 LABELS cpython llvm c)
RUN(NAME expr_05 LABELS cpython llvm)
RUN(NAME expr_06 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
17 changes: 17 additions & 0 deletions integration_tests/expr_06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from ltypes import i32, f32
from numpy import empty

def main0():
x: i32 = 25
y: i32 = (2 + 3) * 5
z: f32 = (2.0 + 3) * 5.0
xa: i32[3] = empty(3)
assert x == 25
assert y == 25
assert z == 25.0

main0()

# Not implemented yet in LPython:
#if __name__ == "__main__":
# main()
14 changes: 7 additions & 7 deletions integration_tests/structs_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def f(pa: Pointer[A]):
print(pa.y)

def g():
x: A
x = A(5, 5.5)
px: Pointer[A]
px = pointer(x)
px.x = 5
px.y = 5.5
f(px)
x: A = A(3, 3.25)
xp: Pointer[A] = pointer(x)
assert xp.x == 3
assert xp.y == 3.25
xp.x = 5
xp.y = 5.5
f(xp)

g()
10 changes: 5 additions & 5 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
+ "' not supported", {v.base.base.loc}, "");
throw Abort();
}
if (v.m_symbolic_value) {
this->visit_expr(*v.m_symbolic_value);
std::string init = src;
sub += "=" + init;
}
}
if (v.m_symbolic_value) {
this->visit_expr(*v.m_symbolic_value);
std::string init = src;
sub += "=" + init;
}
return sub;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ R"(#include <stdio.h>
std::string sub;
ASR::Variable_t *return_var = LFortran::ASRUtils::EXPR2VAR(x.m_return_var);
if (ASRUtils::is_integer(*return_var->m_type)) {
int kind = ASR::down_cast<ASR::Integer_t>(return_var->m_type)->m_kind;
int kind = ASR::down_cast<ASR::Integer_t>(return_var->m_type)->m_kind;
switch (kind) {
case (1) : sub = "int8_t "; break;
case (2) : sub = "int16_t "; break;
Expand Down
3 changes: 2 additions & 1 deletion src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
#include <libasr/codegen/llvm_array_utils.h>

// Uncomment for ASR printing below
//#include <lfortran/pickle.h>
// #include <lpython/pickle.h>

#if LLVM_VERSION_MAJOR >= 11
# define FIXED_VECTOR_TYPE llvm::FixedVectorType
Expand Down Expand Up @@ -1712,6 +1712,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
if( v->m_symbolic_value != nullptr &&
!ASR::is_a<ASR::List_t>(*v->m_type)) {
target_var = ptr;
tmp = nullptr;
this->visit_expr_wrapper(v->m_symbolic_value, true);
llvm::Value *init_value = tmp;
if (ASR::is_a<ASR::ArrayConstant_t>(*v->m_symbolic_value)) {
Expand Down
28 changes: 26 additions & 2 deletions src/libasr/pass/class_constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,36 @@ class ClassConstructorVisitor : public PassUtils::PassVisitor<ClassConstructorVi

public:

bool is_constructor_present;
bool is_constructor_present, is_init_constructor;

ClassConstructorVisitor(Allocator &al) : PassVisitor(al, nullptr),
result_var(nullptr), is_constructor_present(false) {
result_var(nullptr), is_constructor_present(false),
is_init_constructor(false) {
pass_result.reserve(al, 0);
}

void visit_Subroutine(const ASR::Subroutine_t &x) {
// FIXME: this is a hack, we need to pass in a non-const `x`,
// which requires to generate a TransformVisitor.
ASR::Subroutine_t &xx = const_cast<ASR::Subroutine_t&>(x);
current_scope = xx.m_symtab;
for( auto item: current_scope->get_scope() ) {
if( ASR::is_a<ASR::Variable_t>(*item.second) ) {
ASR::Variable_t* variable = ASR::down_cast<ASR::Variable_t>(item.second);
if( variable->m_symbolic_value ) {
result_var = ASRUtils::EXPR(ASR::make_Var_t(al, variable->base.base.loc,
item.second));
is_init_constructor = false;
this->visit_expr(*variable->m_symbolic_value);
if( is_init_constructor ) {
variable->m_symbolic_value = nullptr;
}
}
}
}
transform_stmts(xx.m_body, xx.n_body);
}

void visit_Assignment(const ASR::Assignment_t& x) {
if( x.m_value->type == ASR::exprType::DerivedTypeConstructor ) {
is_constructor_present = true;
Expand All @@ -37,6 +60,7 @@ class ClassConstructorVisitor : public PassUtils::PassVisitor<ClassConstructorVi
}

void visit_DerivedTypeConstructor(const ASR::DerivedTypeConstructor_t &x) {
is_init_constructor = true;
if( x.n_args == 0 ) {
remove_original_stmt = true;
}
Expand Down
7 changes: 7 additions & 0 deletions src/libasr/pass/pass_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ namespace LFortran {
void transform_stmts(ASR::stmt_t **&m_body, size_t &n_body) {
Vec<ASR::stmt_t*> body;
body.reserve(al, n_body);
if (pass_result.size() > 0) {
asr_changed = true;
for (size_t j=0; j < pass_result.size(); j++) {
body.push_back(al, pass_result[j]);
}
pass_result.n = 0;
}
for (size_t i=0; i<n_body; i++) {
// Not necessary after we check it after each visit_stmt in every
// visitor method:
Expand Down
7 changes: 6 additions & 1 deletion src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,8 +1319,11 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {

ASR::expr_t *value = nullptr;
ASR::expr_t *init_expr = nullptr;
tmp = nullptr;
if (x.m_value) {
this->visit_expr(*x.m_value);
}
if (tmp) {
value = ASRUtils::EXPR(tmp);
value = cast_helper(type, value, true);
if (!ASRUtils::check_equal_type(type, ASRUtils::expr_type(value))) {
Expand All @@ -1336,6 +1339,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
throw SemanticAbort();
}
init_expr = value;
value = ASRUtils::expr_value(value);
}
ASR::intentType s_intent = ASRUtils::intent_local;
ASR::storage_typeType storage_type =
Expand Down Expand Up @@ -2899,8 +2903,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
if (ASRUtils::expr_value(left) != nullptr &&
ASRUtils::expr_value(right) != nullptr) {
if (ASRUtils::is_integer(*source_type)) {
ASR::expr_t* left_value_expr = ASRUtils::expr_value(left);
int64_t left_value = ASR::down_cast<ASR::IntegerConstant_t>(
ASRUtils::expr_value(left))
left_value_expr)
->m_n;
int64_t right_value = ASR::down_cast<ASR::IntegerConstant_t>(
ASRUtils::expr_value(right))
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-assign2-8d1a2ee.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-assign2-8d1a2ee.stdout",
"stdout_hash": "4aa03ce3bb76f9146f5e3e9da1df04ebf0f106625a2a468793f06419",
"stdout_hash": "b358f13ad062675500fda44dc9108ac54f4d4a6dde14422717b86567",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-assign2-8d1a2ee.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {f: (Variable 1 f Local (Cast (RealConstant 1.23456788999999989e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 1.23456788999999989e+00 (Real 4 []))) (Cast (RealConstant 1.23456788999999989e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 1.23456788999999989e+00 (Real 4 []))) Default (Real 4 []) Source Public Required .false.), f2: (Variable 1 f2 Local (RealConstant 1.23456789012340007e+00 (Real 8 [])) (RealConstant 1.23456789012340007e+00 (Real 8 [])) Default (Real 8 []) Source Public Required .false.), i: (Variable 1 i Local (IntegerConstant 5 (Integer 4 [])) (IntegerConstant 5 (Integer 4 [])) Default (Integer 4 []) Source Public Required .false.), i2: (Variable 1 i2 Local (Cast (IntegerConstant 53430903434 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) (Cast (IntegerConstant 53430903434 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) Default (Integer 8 []) Source Public Required .false.), main_program: (Program (SymbolTable 2 {}) main_program [] [])}) [])
(TranslationUnit (SymbolTable 1 {f: (Variable 1 f Local (Cast (RealConstant 1.23456788999999989e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 1.23456788999999989e+00 (Real 4 []))) (RealConstant 1.23456788999999989e+00 (Real 4 [])) Default (Real 4 []) Source Public Required .false.), f2: (Variable 1 f2 Local (RealConstant 1.23456789012340007e+00 (Real 8 [])) (RealConstant 1.23456789012340007e+00 (Real 8 [])) Default (Real 8 []) Source Public Required .false.), i: (Variable 1 i Local (IntegerConstant 5 (Integer 4 [])) (IntegerConstant 5 (Integer 4 [])) Default (Integer 4 []) Source Public Required .false.), i2: (Variable 1 i2 Local (Cast (IntegerConstant 53430903434 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) () Default (Integer 8 []) Source Public Required .false.), main_program: (Program (SymbolTable 2 {}) main_program [] [])}) [])
4 changes: 2 additions & 2 deletions tests/reference/asr-structs_03-0cef911.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-structs_03-0cef911",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/structs_03.py",
"infile_hash": "64975e5bcbb620c3ed53f38dd8322dcaa6a38577b288abc909639645",
"infile_hash": "745be61ec57b0a39c6f981dadeb4d8f2cf9d5aef9ca00ab856510795",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-structs_03-0cef911.stdout",
"stdout_hash": "edf92b0144446b9950ae26d366deb99a293358840a200b975739d0c7",
"stdout_hash": "1028ceedc469db4b7760b842a14fddab5bf06056b0e80709748e9b3c",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_03-0cef911.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {A: (DerivedType (SymbolTable 2 {x: (Variable 2 x Local () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 2 y Local () () Default (Real 4 []) Source Public Required .false.)}) A [x y] Source Public ()), _lpython_main_program: (Subroutine (SymbolTable 6 {}) _lpython_main_program [] [(SubroutineCall 1 g () [] ())] Source Public Implementation () .false. .false.), f: (Subroutine (SymbolTable 3 {pa: (Variable 3 pa In () () Default (Pointer (Derived 1 A [])) Source Public Required .false.)}) f [(Var 3 pa)] [(Print () [(DerivedRef (Var 3 pa) 2 x (Integer 4 []) ())]) (Print () [(DerivedRef (Var 3 pa) 2 y (Real 4 []) ())])] Source Public Implementation () .false. .false.), g: (Subroutine (SymbolTable 4 {px: (Variable 4 px Local () () Default (Pointer (Derived 1 A [])) Source Public Required .false.), x: (Variable 4 x Local () () Default (Derived 1 A []) Source Public Required .false.)}) g [] [(= (Var 4 x) (DerivedTypeConstructor 1 A [(IntegerConstant 5 (Integer 4 [])) (Cast (RealConstant 5.50000000000000000e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 5.50000000000000000e+00 (Real 4 [])))] (Derived 1 A []) ()) ()) (= (Var 4 px) (GetPointer (Var 4 x) (Pointer (Derived 1 A [])) ()) ()) (= (DerivedRef (Var 4 px) 2 x (Integer 4 []) ()) (IntegerConstant 5 (Integer 4 [])) ()) (= (DerivedRef (Var 4 px) 2 y (Real 4 []) ()) (Cast (RealConstant 5.50000000000000000e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 5.50000000000000000e+00 (Real 4 []))) ()) (SubroutineCall 1 f () [((Var 4 px))] ())] Source Public Implementation () .false. .false.), main_program: (Program (SymbolTable 5 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])
(TranslationUnit (SymbolTable 1 {A: (DerivedType (SymbolTable 2 {x: (Variable 2 x Local () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 2 y Local () () Default (Real 4 []) Source Public Required .false.)}) A [x y] Source Public ()), _lpython_main_program: (Subroutine (SymbolTable 6 {}) _lpython_main_program [] [(SubroutineCall 1 g () [] ())] Source Public Implementation () .false. .false.), f: (Subroutine (SymbolTable 3 {pa: (Variable 3 pa In () () Default (Pointer (Derived 1 A [])) Source Public Required .false.)}) f [(Var 3 pa)] [(Print () [(DerivedRef (Var 3 pa) 2 x (Integer 4 []) ())]) (Print () [(DerivedRef (Var 3 pa) 2 y (Real 4 []) ())])] Source Public Implementation () .false. .false.), g: (Subroutine (SymbolTable 4 {x: (Variable 4 x Local (DerivedTypeConstructor 1 A [(IntegerConstant 3 (Integer 4 [])) (Cast (RealConstant 3.25000000000000000e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 3.25000000000000000e+00 (Real 4 [])))] (Derived 1 A []) ()) () Default (Derived 1 A []) Source Public Required .false.), xp: (Variable 4 xp Local (GetPointer (Var 4 x) (Pointer (Derived 1 A [])) ()) () Default (Pointer (Derived 1 A [])) Source Public Required .false.)}) g [] [(Assert (Compare (DerivedRef (Var 4 xp) 2 x (Integer 4 []) ()) Eq (IntegerConstant 3 (Integer 4 [])) (Logical 4 []) () ()) ()) (Assert (Compare (Cast (DerivedRef (Var 4 xp) 2 y (Real 4 []) ()) RealToReal (Real 8 []) ()) Eq (RealConstant 3.25000000000000000e+00 (Real 8 [])) (Logical 4 []) () ()) ()) (= (DerivedRef (Var 4 xp) 2 x (Integer 4 []) ()) (IntegerConstant 5 (Integer 4 [])) ()) (= (DerivedRef (Var 4 xp) 2 y (Real 4 []) ()) (Cast (RealConstant 5.50000000000000000e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 5.50000000000000000e+00 (Real 4 []))) ()) (SubroutineCall 1 f () [((Var 4 xp))] ())] Source Public Implementation () .false. .false.), main_program: (Program (SymbolTable 5 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])