Skip to content

Commit 998e8ba

Browse files
authored
Merge pull request #642 from czgdp1807/init_decl
Fixing initialisation at declaration issues
2 parents d010d6b + 392a860 commit 998e8ba

13 files changed

+77
-22
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ RUN(NAME expr_02 LABELS cpython llvm c)
131131
RUN(NAME expr_03 LABELS cpython llvm c)
132132
RUN(NAME expr_04 LABELS cpython llvm c)
133133
RUN(NAME expr_05 LABELS cpython llvm)
134+
RUN(NAME expr_06 LABELS cpython llvm)
134135
RUN(NAME test_types_01 LABELS cpython llvm)
135136
RUN(NAME test_str_01 LABELS cpython llvm)
136137
RUN(NAME test_str_02 LABELS cpython llvm)

integration_tests/expr_06.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from ltypes import i32, f32
2+
from numpy import empty
3+
4+
def main0():
5+
x: i32 = 25
6+
y: i32 = (2 + 3) * 5
7+
z: f32 = (2.0 + 3) * 5.0
8+
xa: i32[3] = empty(3)
9+
assert x == 25
10+
assert y == 25
11+
assert z == 25.0
12+
13+
main0()
14+
15+
# Not implemented yet in LPython:
16+
#if __name__ == "__main__":
17+
# main()

integration_tests/structs_03.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ def f(pa: Pointer[A]):
1010
print(pa.y)
1111

1212
def g():
13-
x: A
14-
x = A(5, 5.5)
15-
px: Pointer[A]
16-
px = pointer(x)
17-
px.x = 5
18-
px.y = 5.5
19-
f(px)
13+
x: A = A(3, 3.25)
14+
xp: Pointer[A] = pointer(x)
15+
assert xp.x == 3
16+
assert xp.y == 3.25
17+
xp.x = 5
18+
xp.y = 5.5
19+
f(xp)
2020

2121
g()

src/libasr/codegen/asr_to_c.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
145145
+ "' not supported", {v.base.base.loc}, "");
146146
throw Abort();
147147
}
148-
if (v.m_symbolic_value) {
149-
this->visit_expr(*v.m_symbolic_value);
150-
std::string init = src;
151-
sub += "=" + init;
152-
}
148+
}
149+
if (v.m_symbolic_value) {
150+
this->visit_expr(*v.m_symbolic_value);
151+
std::string init = src;
152+
sub += "=" + init;
153153
}
154154
return sub;
155155
}

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ R"(#include <stdio.h>
252252
std::string sub;
253253
ASR::Variable_t *return_var = LFortran::ASRUtils::EXPR2VAR(x.m_return_var);
254254
if (ASRUtils::is_integer(*return_var->m_type)) {
255-
int kind = ASR::down_cast<ASR::Integer_t>(return_var->m_type)->m_kind;
255+
int kind = ASR::down_cast<ASR::Integer_t>(return_var->m_type)->m_kind;
256256
switch (kind) {
257257
case (1) : sub = "int8_t "; break;
258258
case (2) : sub = "int16_t "; break;

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
#include <libasr/codegen/llvm_array_utils.h>
6767

6868
// Uncomment for ASR printing below
69-
//#include <lfortran/pickle.h>
69+
// #include <lpython/pickle.h>
7070

7171
#if LLVM_VERSION_MAJOR >= 11
7272
# define FIXED_VECTOR_TYPE llvm::FixedVectorType
@@ -1712,6 +1712,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
17121712
if( v->m_symbolic_value != nullptr &&
17131713
!ASR::is_a<ASR::List_t>(*v->m_type)) {
17141714
target_var = ptr;
1715+
tmp = nullptr;
17151716
this->visit_expr_wrapper(v->m_symbolic_value, true);
17161717
llvm::Value *init_value = tmp;
17171718
if (ASR::is_a<ASR::ArrayConstant_t>(*v->m_symbolic_value)) {

src/libasr/pass/class_constructor.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,36 @@ class ClassConstructorVisitor : public PassUtils::PassVisitor<ClassConstructorVi
2121

2222
public:
2323

24-
bool is_constructor_present;
24+
bool is_constructor_present, is_init_constructor;
2525

2626
ClassConstructorVisitor(Allocator &al) : PassVisitor(al, nullptr),
27-
result_var(nullptr), is_constructor_present(false) {
27+
result_var(nullptr), is_constructor_present(false),
28+
is_init_constructor(false) {
2829
pass_result.reserve(al, 0);
2930
}
3031

32+
void visit_Subroutine(const ASR::Subroutine_t &x) {
33+
// FIXME: this is a hack, we need to pass in a non-const `x`,
34+
// which requires to generate a TransformVisitor.
35+
ASR::Subroutine_t &xx = const_cast<ASR::Subroutine_t&>(x);
36+
current_scope = xx.m_symtab;
37+
for( auto item: current_scope->get_scope() ) {
38+
if( ASR::is_a<ASR::Variable_t>(*item.second) ) {
39+
ASR::Variable_t* variable = ASR::down_cast<ASR::Variable_t>(item.second);
40+
if( variable->m_symbolic_value ) {
41+
result_var = ASRUtils::EXPR(ASR::make_Var_t(al, variable->base.base.loc,
42+
item.second));
43+
is_init_constructor = false;
44+
this->visit_expr(*variable->m_symbolic_value);
45+
if( is_init_constructor ) {
46+
variable->m_symbolic_value = nullptr;
47+
}
48+
}
49+
}
50+
}
51+
transform_stmts(xx.m_body, xx.n_body);
52+
}
53+
3154
void visit_Assignment(const ASR::Assignment_t& x) {
3255
if( x.m_value->type == ASR::exprType::DerivedTypeConstructor ) {
3356
is_constructor_present = true;
@@ -37,6 +60,7 @@ class ClassConstructorVisitor : public PassUtils::PassVisitor<ClassConstructorVi
3760
}
3861

3962
void visit_DerivedTypeConstructor(const ASR::DerivedTypeConstructor_t &x) {
63+
is_init_constructor = true;
4064
if( x.n_args == 0 ) {
4165
remove_original_stmt = true;
4266
}

src/libasr/pass/pass_utils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ namespace LFortran {
8484
void transform_stmts(ASR::stmt_t **&m_body, size_t &n_body) {
8585
Vec<ASR::stmt_t*> body;
8686
body.reserve(al, n_body);
87+
if (pass_result.size() > 0) {
88+
asr_changed = true;
89+
for (size_t j=0; j < pass_result.size(); j++) {
90+
body.push_back(al, pass_result[j]);
91+
}
92+
pass_result.n = 0;
93+
}
8794
for (size_t i=0; i<n_body; i++) {
8895
// Not necessary after we check it after each visit_stmt in every
8996
// visitor method:

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,8 +1319,11 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
13191319

13201320
ASR::expr_t *value = nullptr;
13211321
ASR::expr_t *init_expr = nullptr;
1322+
tmp = nullptr;
13221323
if (x.m_value) {
13231324
this->visit_expr(*x.m_value);
1325+
}
1326+
if (tmp) {
13241327
value = ASRUtils::EXPR(tmp);
13251328
value = cast_helper(type, value, true);
13261329
if (!ASRUtils::check_equal_type(type, ASRUtils::expr_type(value))) {
@@ -1336,6 +1339,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
13361339
throw SemanticAbort();
13371340
}
13381341
init_expr = value;
1342+
value = ASRUtils::expr_value(value);
13391343
}
13401344
ASR::intentType s_intent = ASRUtils::intent_local;
13411345
ASR::storage_typeType storage_type =
@@ -2899,8 +2903,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
28992903
if (ASRUtils::expr_value(left) != nullptr &&
29002904
ASRUtils::expr_value(right) != nullptr) {
29012905
if (ASRUtils::is_integer(*source_type)) {
2906+
ASR::expr_t* left_value_expr = ASRUtils::expr_value(left);
29022907
int64_t left_value = ASR::down_cast<ASR::IntegerConstant_t>(
2903-
ASRUtils::expr_value(left))
2908+
left_value_expr)
29042909
->m_n;
29052910
int64_t right_value = ASR::down_cast<ASR::IntegerConstant_t>(
29062911
ASRUtils::expr_value(right))

tests/reference/asr-assign2-8d1a2ee.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-assign2-8d1a2ee.stdout",
9-
"stdout_hash": "4aa03ce3bb76f9146f5e3e9da1df04ebf0f106625a2a468793f06419",
9+
"stdout_hash": "b358f13ad062675500fda44dc9108ac54f4d4a6dde14422717b86567",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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 [] [])}) [])
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 []))) (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 [] [])}) [])

tests/reference/asr-structs_03-0cef911.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "asr-structs_03-0cef911",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/structs_03.py",
5-
"infile_hash": "64975e5bcbb620c3ed53f38dd8322dcaa6a38577b288abc909639645",
5+
"infile_hash": "745be61ec57b0a39c6f981dadeb4d8f2cf9d5aef9ca00ab856510795",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-structs_03-0cef911.stdout",
9-
"stdout_hash": "edf92b0144446b9950ae26d366deb99a293358840a200b975739d0c7",
9+
"stdout_hash": "1028ceedc469db4b7760b842a14fddab5bf06056b0e80709748e9b3c",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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 () [] ())])}) [])
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 {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 () [] ())])}) [])

0 commit comments

Comments
 (0)