@@ -2526,6 +2526,55 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
2526
2526
pptr, nullptr );
2527
2527
}
2528
2528
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
+
2529
2578
void visit_AnnAssignUtil (const AST::AnnAssign_t& x, std::string& var_name,
2530
2579
bool wrap_derived_type_in_pointer=false ,
2531
2580
ASR::expr_t * init_expr=nullptr , ASR::abiType abi=ASR::abiType::Source) {
@@ -2595,6 +2644,13 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
2595
2644
x.base .base .loc , abi, storage_type);
2596
2645
}
2597
2646
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
+
2598
2654
if ( !is_c_p_pointer_call ) {
2599
2655
tmp = nullptr ;
2600
2656
}
@@ -4676,6 +4732,19 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
4676
4732
tmp_value = ASRUtils::EXPR (ASR::make_ListConstant_t (al, x.base .base .loc , list_ele.p ,
4677
4733
list_ele.size (), target_type));
4678
4734
}
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
+ }
4679
4748
if (!tmp_value) continue ;
4680
4749
ASR::ttype_t *value_type = ASRUtils::expr_type (tmp_value);
4681
4750
if ( ASR::is_a<ASR::Pointer_t>(*target_type) &&
0 commit comments