Skip to content

Commit bcde020

Browse files
committed
Allocate using empty
1 parent f3934e9 commit bcde020

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,6 +2526,55 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
25262526
pptr, nullptr);
25272527
}
25282528

2529+
ASR::asr_t* check_to_allocate_array(AST::expr_t *value, std::string var_name,
2530+
const Location &loc) {
2531+
if (AST::is_a<AST::Call_t>(*value)) {
2532+
AST::Call_t *ct = AST::down_cast<AST::Call_t>(value);
2533+
if (AST::is_a<AST::Name_t>(*ct->m_func)) {
2534+
std::string call_name = AST::down_cast<AST::Name_t>(ct->m_func)->m_id;
2535+
if (call_name == "empty") {
2536+
LCOMPILERS_ASSERT(ct->n_args > 0);
2537+
if (AST::is_a<AST::Tuple_t>(*ct->m_args[0])) {
2538+
AST::Tuple_t *tt = AST::down_cast<AST::Tuple_t>(ct->m_args[0]);
2539+
Vec<ASR::alloc_arg_t> alloc_args_vec;
2540+
alloc_args_vec.reserve(al, 1);
2541+
ASR::alloc_arg_t new_arg;
2542+
new_arg.loc = loc;
2543+
ASR::ttype_t *int32_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
2544+
4, nullptr, 0));
2545+
ASR::expr_t* const_0 = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al,
2546+
loc, 0, int32_type));
2547+
Vec<ASR::dimension_t> dims_vec;
2548+
dims_vec.reserve(al, tt->n_elts);
2549+
for (size_t i=0; i<tt->n_elts; i++) {
2550+
ASR::dimension_t new_dim;
2551+
new_dim.loc = loc;
2552+
this->visit_expr(*tt->m_elts[0]);
2553+
new_dim.m_start = const_0;
2554+
new_dim.m_length = ASRUtils::EXPR(tmp);
2555+
dims_vec.push_back(al, new_dim);
2556+
}
2557+
new_arg.m_dims = dims_vec.p;
2558+
new_arg.n_dims = dims_vec.size();
2559+
ASR::symbol_t *v_sym = current_scope->resolve_symbol(var_name);
2560+
ASR::expr_t* v_expr = ASRUtils::EXPR(ASR::make_Var_t(al,
2561+
loc, v_sym));
2562+
new_arg.m_a = v_expr;
2563+
alloc_args_vec.push_back(al, new_arg);
2564+
tmp = ASR::make_Allocate_t(al, loc,
2565+
alloc_args_vec.p, alloc_args_vec.size(),
2566+
nullptr, nullptr, nullptr);
2567+
return tmp;
2568+
} else {
2569+
throw SemanticError("Only tuple argument is accepted as dimensions "
2570+
"for allocating using empty()", ct->base.base.loc);
2571+
}
2572+
}
2573+
}
2574+
}
2575+
return nullptr;
2576+
}
2577+
25292578
void visit_AnnAssignUtil(const AST::AnnAssign_t& x, std::string& var_name,
25302579
bool wrap_derived_type_in_pointer=false,
25312580
ASR::expr_t* init_expr=nullptr, ASR::abiType abi=ASR::abiType::Source) {
@@ -2595,6 +2644,13 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
25952644
x.base.base.loc, abi, storage_type);
25962645
}
25972646

2647+
if (is_allocatable && x.m_value && AST::is_a<AST::Call_t>(*x.m_value)) {
2648+
tmp = check_to_allocate_array(x.m_value, var_name, x.base.base.loc);
2649+
if( current_body && tmp) {
2650+
current_body->push_back(al, ASRUtils::STMT(tmp));
2651+
}
2652+
}
2653+
25982654
if( !is_c_p_pointer_call ) {
25992655
tmp = nullptr;
26002656
}
@@ -4676,6 +4732,19 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
46764732
tmp_value = ASRUtils::EXPR(ASR::make_ListConstant_t(al, x.base.base.loc, list_ele.p,
46774733
list_ele.size(), target_type));
46784734
}
4735+
if (tmp_value == nullptr && ASR::is_a<ASR::Var_t>(*target)) {
4736+
ASR::Var_t *var_tar = ASR::down_cast<ASR::Var_t>(target);
4737+
if (ASR::is_a<ASR::Variable_t>(*var_tar->m_v)) {
4738+
if (ASR::down_cast<ASR::Variable_t>(var_tar->m_v)->m_storage == ASR::storage_typeType::Allocatable) {
4739+
ASR::asr_t *st = check_to_allocate_array(x.m_value, ASRUtils::symbol_name(var_tar->m_v),
4740+
x.base.base.loc);
4741+
if (st) {
4742+
tmp_vec.push_back(st);
4743+
continue;
4744+
}
4745+
}
4746+
}
4747+
}
46794748
if (!tmp_value) continue;
46804749
ASR::ttype_t *value_type = ASRUtils::expr_type(tmp_value);
46814750
if( ASR::is_a<ASR::Pointer_t>(*target_type) &&

src/runtime/lpython/lpython.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
__slots__ = ["i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64", "c32", "c64", "CPtr",
1010
"overload", "ccall", "TypeVar", "pointer", "c_p_pointer", "Pointer",
1111
"p_c_pointer", "vectorize", "inline", "Union", "static", "with_goto",
12-
"packed", "Const", "sizeof", "ccallable", "ccallback", "Callable"]
12+
"packed", "Const", "sizeof", "ccallable", "ccallback", "Callable",
13+
"Allocatable"]
1314

1415
# data-types
1516

@@ -29,6 +30,7 @@
2930
"c_ptr": lambda x: x,
3031
"Const": lambda x: x,
3132
"Callable": lambda x: x,
33+
"Allocatable": lambda x: x,
3234
"Pointer": lambda x: x,
3335
}
3436

@@ -80,6 +82,7 @@ def __init__(self, type, dims):
8082
CPtr = Type("c_ptr")
8183
Const = ConstType("Const")
8284
Callable = Type("Callable")
85+
Allocatable = Type("Allocatable")
8386
Union = ctypes.Union
8487
Pointer = PointerType("Pointer")
8588

0 commit comments

Comments
 (0)