@@ -498,7 +498,6 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
498
498
Vec<ASR::stmt_t *> *current_body;
499
499
ASR::ttype_t * ann_assign_target_type;
500
500
AST::expr_t * assign_ast_target;
501
- bool is_c_p_pointer_call;
502
501
503
502
std::map<std::string, int > generic_func_nums;
504
503
std::map<std::string, std::map<std::string, ASR::ttype_t *>> generic_func_subs;
@@ -535,7 +534,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
535
534
: diag{diagnostics}, al{al}, lm{lm}, current_scope{symbol_table}, main_module{main_module}, module_name{module_name},
536
535
ast_overload{ast_overload}, parent_dir{parent_dir}, import_paths{import_paths},
537
536
current_body{nullptr }, ann_assign_target_type{nullptr },
538
- assign_ast_target{nullptr }, is_c_p_pointer_call{ false }, allow_implicit_casting{allow_implicit_casting_} {
537
+ assign_ast_target{nullptr }, allow_implicit_casting{allow_implicit_casting_} {
539
538
current_module_dependencies.reserve (al, 4 );
540
539
global_init.reserve (al, 1 );
541
540
}
@@ -2674,14 +2673,22 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
2674
2673
return std::string (base_name->m_id ) == " Union" ;
2675
2674
}
2676
2675
2677
- void create_add_variable_to_scope (std::string& var_name, ASR::expr_t * init_expr,
2678
- ASR::ttype_t * type, const Location& loc, ASR::abiType abi,
2679
- ASR::storage_typeType storage_type=ASR::storage_typeType::Default) {
2680
-
2676
+ void process_variable_init_val (ASR::symbol_t * v_sym, const Location& loc, ASR::expr_t * init_expr=nullptr ) {
2681
2677
ASR::expr_t * value = nullptr ;
2678
+ ASR::Variable_t* v_variable = ASR::down_cast<ASR::Variable_t>(v_sym);
2679
+ std::string var_name = v_variable->m_name ;
2680
+ ASR::ttype_t * type = v_variable->m_type ;
2682
2681
if ( init_expr ) {
2683
2682
value = ASRUtils::expr_value (init_expr);
2683
+ SetChar variable_dependencies_vec;
2684
+ variable_dependencies_vec.reserve (al, 1 );
2685
+ ASRUtils::collect_variable_dependencies (al, variable_dependencies_vec, type, init_expr, value);
2686
+ v_variable->m_dependencies = variable_dependencies_vec.p ;
2687
+ v_variable->n_dependencies = variable_dependencies_vec.size ();
2688
+ v_variable->m_symbolic_value = init_expr;
2689
+ v_variable->m_value = value;
2684
2690
}
2691
+
2685
2692
bool is_runtime_expression = !ASRUtils::is_value_constant (value);
2686
2693
bool is_variable_const = ASR::is_a<ASR::Const_t>(*type);
2687
2694
@@ -2690,27 +2697,6 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
2690
2697
" is not initialised at declaration." , loc);
2691
2698
}
2692
2699
2693
- ASR::intentType s_intent = ASRUtils::intent_local;
2694
- if ( ASR::is_a<ASR::Const_t>(*type) ) {
2695
- storage_type = ASR::storage_typeType::Parameter;
2696
- }
2697
- ASR::abiType current_procedure_abi_type = abi;
2698
- ASR::accessType s_access = ASR::accessType::Public;
2699
- ASR::presenceType s_presence = ASR::presenceType::Required;
2700
- bool value_attr = false ;
2701
- SetChar variable_dependencies_vec;
2702
- variable_dependencies_vec.reserve (al, 1 );
2703
- ASRUtils::collect_variable_dependencies (al, variable_dependencies_vec, type, init_expr, value);
2704
- ASR::asr_t *v = ASR::make_Variable_t (al, loc, current_scope,
2705
- s2c (al, var_name), variable_dependencies_vec.p ,
2706
- variable_dependencies_vec.size (),
2707
- s_intent, init_expr, value, storage_type, type,
2708
- nullptr ,
2709
- current_procedure_abi_type, s_access, s_presence,
2710
- value_attr);
2711
- ASR::symbol_t * v_sym = ASR::down_cast<ASR::symbol_t >(v);
2712
- ASR::Variable_t* v_variable = ASR::down_cast<ASR::Variable_t>(v_sym);
2713
-
2714
2700
if ( init_expr && (current_body || ASR::is_a<ASR::List_t>(*type) ||
2715
2701
is_runtime_expression) && !is_variable_const) {
2716
2702
ASR::expr_t * v_expr = ASRUtils::EXPR (ASR::make_Var_t (al, loc, v_sym));
@@ -2738,7 +2724,31 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
2738
2724
current_body ) {
2739
2725
throw SemanticError (" Initialisation of " + var_name + " must reduce to a compile time constant." , loc);
2740
2726
}
2727
+ }
2741
2728
2729
+ void create_add_variable_to_scope (std::string& var_name,
2730
+ ASR::ttype_t * type, const Location& loc, ASR::abiType abi,
2731
+ ASR::storage_typeType storage_type=ASR::storage_typeType::Default) {
2732
+
2733
+ ASR::intentType s_intent = ASRUtils::intent_local;
2734
+ if ( ASR::is_a<ASR::Const_t>(*type) ) {
2735
+ storage_type = ASR::storage_typeType::Parameter;
2736
+ }
2737
+ ASR::abiType current_procedure_abi_type = abi;
2738
+ ASR::accessType s_access = ASR::accessType::Public;
2739
+ ASR::presenceType s_presence = ASR::presenceType::Required;
2740
+ bool value_attr = false ;
2741
+ SetChar variable_dependencies_vec;
2742
+ variable_dependencies_vec.reserve (al, 1 );
2743
+ ASRUtils::collect_variable_dependencies (al, variable_dependencies_vec, type);
2744
+ ASR::asr_t *v = ASR::make_Variable_t (al, loc, current_scope,
2745
+ s2c (al, var_name), variable_dependencies_vec.p ,
2746
+ variable_dependencies_vec.size (),
2747
+ s_intent, nullptr , nullptr , storage_type, type,
2748
+ nullptr ,
2749
+ current_procedure_abi_type, s_access, s_presence,
2750
+ value_attr);
2751
+ ASR::symbol_t * v_sym = ASR::down_cast<ASR::symbol_t >(v);
2742
2752
current_scope->add_or_overwrite_symbol (var_name, v_sym);
2743
2753
}
2744
2754
@@ -3012,11 +3022,12 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
3012
3022
type = ASRUtils::TYPE (ASR::make_Allocatable_t (al, type->base .loc ,
3013
3023
ASRUtils::type_get_past_pointer (type)));
3014
3024
}
3015
- bool is_c_p_pointer_call_copy = is_c_p_pointer_call;
3016
- ASR::expr_t *value = nullptr ;
3025
+
3026
+ create_add_variable_to_scope (var_name, type,
3027
+ x.base .base .loc , abi, storage_type);
3028
+
3017
3029
if ( !init_expr ) {
3018
3030
tmp = nullptr ;
3019
- is_c_p_pointer_call = false ;
3020
3031
if (x.m_value ) {
3021
3032
this ->visit_expr (*x.m_value );
3022
3033
} else {
@@ -3026,13 +3037,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
3026
3037
ASRUtils::type_to_str_python (type), x.base .base .loc );
3027
3038
}
3028
3039
}
3029
- if ( is_c_p_pointer_call ) {
3030
- create_add_variable_to_scope (var_name, nullptr , type,
3031
- x.base .base .loc , abi, storage_type);
3032
- AST::Call_t* c_p_pointer_call = AST::down_cast<AST::Call_t>(x.m_value );
3033
- tmp = create_CPtrToPointer (*c_p_pointer_call);
3034
- } else if (tmp) {
3035
- value = ASRUtils::EXPR (tmp);
3040
+ if (tmp && ASR::is_a<ASR::expr_t >(*tmp)) {
3041
+ ASR::expr_t * value = ASRUtils::EXPR (tmp);
3036
3042
ASR::ttype_t * underlying_type = type;
3037
3043
if ( ASR::is_a<ASR::Const_t>(*type) ) {
3038
3044
underlying_type = ASRUtils::get_contained_type (type);
@@ -3056,14 +3062,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
3056
3062
cast_helper (type, init_expr, init_expr->base .loc );
3057
3063
}
3058
3064
3059
- if ( !is_c_p_pointer_call ) {
3060
- if (inside_struct && !ASR::is_a<ASR::Const_t>(*type)) {
3061
- create_add_variable_to_scope (var_name, nullptr , type,
3062
- x.base .base .loc , abi, storage_type);
3063
- } else {
3064
- create_add_variable_to_scope (var_name, init_expr, type,
3065
- x.base .base .loc , abi, storage_type);
3066
- }
3065
+ if (!inside_struct || ASR::is_a<ASR::Const_t>(*type)) {
3066
+ process_variable_init_val (current_scope->get_symbol (var_name), x.base .base .loc , init_expr);
3067
3067
}
3068
3068
3069
3069
if (is_allocatable && x.m_value && AST::is_a<AST::Call_t>(*x.m_value )) {
@@ -3073,10 +3073,10 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
3073
3073
}
3074
3074
}
3075
3075
3076
- if ( !is_c_p_pointer_call ) {
3076
+ if ( !(tmp && ASR::is_a<ASR::stmt_t >(*tmp) &&
3077
+ ASR::is_a<ASR::CPtrToPointer_t>(*ASR::down_cast<ASR::stmt_t >(tmp))) ) {
3077
3078
tmp = nullptr ;
3078
3079
}
3079
- is_c_p_pointer_call = is_c_p_pointer_call_copy;
3080
3080
ann_assign_target_type = ann_assign_target_type_copy;
3081
3081
}
3082
3082
@@ -5243,18 +5243,13 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
5243
5243
ASR::expr_t *target, *assign_value = nullptr , *tmp_value;
5244
5244
AST::expr_t * assign_ast_target_copy = assign_ast_target;
5245
5245
assign_ast_target = x.m_targets [0 ];
5246
- bool is_c_p_pointer_call_copy = is_c_p_pointer_call;
5247
- is_c_p_pointer_call = false ;
5248
5246
this ->visit_expr (*x.m_value );
5249
- if ( is_c_p_pointer_call ) {
5250
- LCOMPILERS_ASSERT (x.n_targets == 1 );
5251
- AST::Call_t* c_p_pointer_call = AST::down_cast<AST::Call_t>(x.m_value );
5252
- tmp = create_CPtrToPointer (*c_p_pointer_call);
5253
- return ;
5254
- }
5255
- is_c_p_pointer_call = is_c_p_pointer_call_copy;
5256
5247
assign_ast_target = assign_ast_target_copy;
5257
5248
if (tmp) {
5249
+ if (ASR::is_a<ASR::stmt_t >(*tmp)) {
5250
+ // This happens for c_p_pointer()
5251
+ return ;
5252
+ }
5258
5253
// This happens if `m.m_value` is `empty`, such as in:
5259
5254
// a = empty(16)
5260
5255
// We skip this statement for now, the array is declared
@@ -7462,20 +7457,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
7462
7457
if (AST::is_a<AST::Name_t>(*x.m_func )) {
7463
7458
AST::Name_t *n = AST::down_cast<AST::Name_t>(x.m_func );
7464
7459
call_name = n->m_id ;
7465
- }
7466
- if (call_name == " c_p_pointer" &&
7467
- !current_scope->resolve_symbol (call_name)) {
7468
- is_c_p_pointer_call = true ;
7469
- tmp = nullptr ;
7470
- return ;
7471
- }
7472
-
7473
- if (AST::is_a<AST::Attribute_t>(*x.m_func )) {
7460
+ } else if (AST::is_a<AST::Attribute_t>(*x.m_func )) {
7474
7461
parse_args (x, args);
7475
7462
AST::Attribute_t *at = AST::down_cast<AST::Attribute_t>(x.m_func );
7476
7463
handle_attribute (at, args, x.base .base .loc );
7477
7464
return ;
7478
- } else if ( call_name == " " ) {
7465
+ } else {
7479
7466
throw SemanticError (" Only Name or Attribute type supported in Call" ,
7480
7467
x.base .base .loc );
7481
7468
}
@@ -7565,6 +7552,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
7565
7552
// with the type
7566
7553
tmp = nullptr ;
7567
7554
return ;
7555
+ } else if (call_name == " c_p_pointer" ) {
7556
+ tmp = create_CPtrToPointer (x);
7557
+ return ;
7568
7558
} else if (call_name == " empty_c_void_p" ) {
7569
7559
// TODO: check that `empty_c_void_p uses` has arguments that are compatible
7570
7560
// with the type
0 commit comments