Skip to content

Commit fe11ae6

Browse files
committed
Updated AST -> ASR transition to handle call to reshape
1 parent 19e352d commit fe11ae6

4 files changed

Lines changed: 32 additions & 7 deletions

File tree

src/libasr/pass/arr_slice.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,9 @@ class ArrSliceVisitor : public PassUtils::PassVisitor<ArrSliceVisitor>
207207
}
208208

209209
void visit_Assignment(const ASR::Assignment_t& x) {
210-
if( ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_target)) &&
211-
ASR::is_a<ASR::GetPointer_t>(*x.m_value) ) {
210+
if( (ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_target)) &&
211+
ASR::is_a<ASR::GetPointer_t>(*x.m_value)) ||
212+
ASR::is_a<ASR::ArrayReshape_t>(*x.m_value) ) {
212213
return ;
213214
}
214215
this->visit_expr(*x.m_value);

src/libasr/pass/array_op.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,9 @@ class ArrayOpVisitor : public PassUtils::PassVisitor<ArrayOpVisitor>
232232
}
233233

234234
void visit_Assignment(const ASR::Assignment_t& x) {
235-
if( ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_target)) &&
236-
ASR::is_a<ASR::GetPointer_t>(*x.m_value) ) {
235+
if( (ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_target)) &&
236+
ASR::is_a<ASR::GetPointer_t>(*x.m_value)) ||
237+
ASR::is_a<ASR::ArrayReshape_t>(*x.m_value) ) {
237238
return ;
238239
}
239240
if( PassUtils::is_array(x.m_target) ) {

src/libasr/pass/implied_do_loops.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ class ImpliedDoLoopVisitor : public PassUtils::PassVisitor<ImpliedDoLoopVisitor>
150150
}
151151

152152
void visit_Assignment(const ASR::Assignment_t &x) {
153-
if( ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_target)) &&
154-
ASR::is_a<ASR::GetPointer_t>(*x.m_value) ) {
153+
if( (ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_target)) &&
154+
ASR::is_a<ASR::GetPointer_t>(*x.m_value)) ||
155+
ASR::is_a<ASR::ArrayReshape_t>(*x.m_value) ) {
155156
return ;
156157
}
157158
if( x.m_value->type == ASR::exprType::ArrayConstant ) {

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030

3131
namespace LFortran::LPython {
32-
32+
3333
// Does a CPython style lookup for a module:
3434
// * First the current directory (this is incorrect, we need to do it relative to the current file)
3535
// * Then the LPython runtime directory
@@ -3657,6 +3657,25 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
36573657
throw SemanticError("len() is only supported for `str`, `set`, `dict`, `list` and `tuple`", loc);
36583658
}
36593659

3660+
ASR::asr_t* handle_reshape(Allocator &al, Vec<ASR::call_arg_t> args,
3661+
const Location &loc) {
3662+
if( args.size() != 2 ) {
3663+
throw SemanticError("reshape accepts only 2 arguments, got " +
3664+
std::to_string(args.size()) + " arguments instead.",
3665+
loc);
3666+
}
3667+
ASR::expr_t* array = args[0].m_value;
3668+
ASR::expr_t* newshape = args[1].m_value;
3669+
Vec<ASR::dimension_t> dims;
3670+
dims.reserve(al, 1);
3671+
ASR::dimension_t newdim;
3672+
newdim.loc = loc;
3673+
newdim.m_start = nullptr, newdim.m_length = nullptr;
3674+
dims.push_back(al, newdim);
3675+
ASR::ttype_t* empty_type = ASRUtils::duplicate_type(al, ASRUtils::expr_type(array), &dims);
3676+
return ASR::make_ArrayReshape_t(al, loc, array, newshape, empty_type, nullptr);
3677+
}
3678+
36603679
ASR::asr_t* create_CPtrToPointer(const AST::Call_t& x) {
36613680
if( x.n_args != 2 ) {
36623681
throw SemanticError("c_p_pointer accepts two positional arguments, "
@@ -3790,6 +3809,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
37903809
// with the type
37913810
tmp = nullptr;
37923811
return;
3812+
} else if (call_name == "reshape") {
3813+
tmp = handle_reshape(al, args, x.base.base.loc);
3814+
return ;
37933815
} else if (call_name == "empty_c_void_p") {
37943816
// TODO: check that `empty_c_void_p uses` has arguments that are compatible
37953817
// with the type

0 commit comments

Comments
 (0)