@@ -2793,27 +2793,33 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
2793
2793
ASR::arraystorageType::RowMajor)); \
2794
2794
} \
2795
2795
2796
- ASR::asr_t * create_CPtrToPointerFromArgs (AST::expr_t * ast_cptr, AST::expr_t * ast_pptr,
2797
- AST::expr_t * ast_type_expr, AST::expr_t * ast_target_shape, const Location& loc) {
2798
- this ->visit_expr (*ast_cptr);
2796
+ ASR::asr_t * create_CPtrToPointer (const AST::Call_t& x) {
2797
+ if ( x.n_args != 2 && x.n_args != 3 ) {
2798
+ throw SemanticError (" c_p_pointer accepts maximum three positional arguments, "
2799
+ " first a variable of c_ptr type, second "
2800
+ " the target type of the first variable and "
2801
+ " third optionally the shape of the target variable "
2802
+ " if target variable is an array" ,
2803
+ x.base .base .loc );
2804
+ }
2805
+ this ->visit_expr (*x.m_args [0 ]);
2799
2806
ASR::expr_t * cptr = ASRUtils::EXPR (tmp);
2800
- this ->visit_expr (*ast_pptr );
2807
+ this ->visit_expr (*assign_ast_target );
2801
2808
ASR::expr_t * pptr = ASRUtils::EXPR (tmp);
2802
2809
ASR::expr_t * target_shape = nullptr ;
2803
- if ( ast_target_shape ) {
2804
- this ->visit_expr (*ast_target_shape );
2810
+ if ( x. n_args == 3 ) {
2811
+ this ->visit_expr (*x. m_args [ 2 ] );
2805
2812
target_shape = ASRUtils::EXPR (tmp);
2806
2813
}
2807
2814
bool is_allocatable = false ;
2808
- ASR::ttype_t * asr_alloc_type = ast_expr_to_asr_type (ast_type_expr->base .loc , *ast_type_expr,
2809
- is_allocatable, true );
2815
+ ASR::ttype_t * asr_alloc_type = ast_expr_to_asr_type (x.m_args [1 ]->base .loc , *x.m_args [1 ], is_allocatable);
2810
2816
ASR::ttype_t * target_type = ASRUtils::type_get_past_pointer (ASRUtils::expr_type (pptr));
2811
2817
if ( !ASRUtils::types_equal (target_type, asr_alloc_type, true ) ) {
2812
2818
diag.add (diag::Diagnostic (
2813
2819
" Type mismatch in c_p_pointer and target variable, the types must match" ,
2814
2820
diag::Level::Error, diag::Stage::Semantic, {
2815
- diag::Label (" type mismatch between target variable type "
2816
- " and c_p_pointer allocation type )" ,
2821
+ diag::Label (" type mismatch (' " + ASRUtils::type_to_str_python (target_type) +
2822
+ " ' and ' " + ASRUtils::type_to_str_python (asr_alloc_type) + " ' )" ,
2817
2823
{target_type->base .loc , asr_alloc_type->base .loc })
2818
2824
})
2819
2825
);
@@ -2837,8 +2843,10 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
2837
2843
}
2838
2844
}
2839
2845
}
2846
+ const Location& loc = x.base .base .loc ;
2840
2847
fill_shape_and_lower_bound_for_CPtrToPointer ();
2841
- return ASR::make_CPtrToPointer_t (al, loc, cptr, pptr, target_shape, lower_bounds);
2848
+ return ASR::make_CPtrToPointer_t (al, loc, cptr,
2849
+ pptr, target_shape, lower_bounds);
2842
2850
}
2843
2851
2844
2852
ASR::asr_t * check_to_allocate_array (AST::expr_t *value, std::string var_name,
@@ -3022,18 +3030,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
3022
3030
create_add_variable_to_scope (var_name, nullptr , type,
3023
3031
x.base .base .loc , abi, storage_type);
3024
3032
AST::Call_t* c_p_pointer_call = AST::down_cast<AST::Call_t>(x.m_value );
3025
- AST::expr_t * cptr = c_p_pointer_call->m_args [0 ];
3026
- AST::expr_t * pptr = assign_ast_target;
3027
- AST::expr_t * pptr_shape = nullptr ;
3028
- if ( c_p_pointer_call->n_args == 3 &&
3029
- c_p_pointer_call->m_args [2 ] != nullptr ) {
3030
- pptr_shape = c_p_pointer_call->m_args [2 ];
3031
- }
3032
- tmp = create_CPtrToPointerFromArgs (cptr, pptr, c_p_pointer_call->m_args [1 ],
3033
- pptr_shape, x.base .base .loc );
3034
- // if( current_body ) {
3035
- // current_body->push_back(al, ASRUtils::STMT(tmp));
3036
- // }
3033
+ tmp = create_CPtrToPointer (*c_p_pointer_call);
3037
3034
} else if (tmp) {
3038
3035
value = ASRUtils::EXPR (tmp);
3039
3036
ASR::ttype_t * underlying_type = type;
@@ -5244,24 +5241,19 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
5244
5241
5245
5242
void visit_Assign (const AST::Assign_t &x) {
5246
5243
ASR::expr_t *target, *assign_value = nullptr , *tmp_value;
5244
+ AST::expr_t * assign_ast_target_copy = assign_ast_target;
5245
+ assign_ast_target = x.m_targets [0 ];
5247
5246
bool is_c_p_pointer_call_copy = is_c_p_pointer_call;
5248
5247
is_c_p_pointer_call = false ;
5249
5248
this ->visit_expr (*x.m_value );
5250
5249
if ( is_c_p_pointer_call ) {
5251
5250
LCOMPILERS_ASSERT (x.n_targets == 1 );
5252
5251
AST::Call_t* c_p_pointer_call = AST::down_cast<AST::Call_t>(x.m_value );
5253
- AST::expr_t * cptr = c_p_pointer_call->m_args [0 ];
5254
- AST::expr_t * pptr = x.m_targets [0 ];
5255
- AST::expr_t * target_shape = nullptr ;
5256
- if ( c_p_pointer_call->n_args == 3 ) {
5257
- target_shape = c_p_pointer_call->m_args [2 ];
5258
- }
5259
- tmp = create_CPtrToPointerFromArgs (cptr, pptr, c_p_pointer_call->m_args [1 ],
5260
- target_shape, x.base .base .loc );
5261
- is_c_p_pointer_call = is_c_p_pointer_call;
5252
+ tmp = create_CPtrToPointer (*c_p_pointer_call);
5262
5253
return ;
5263
5254
}
5264
5255
is_c_p_pointer_call = is_c_p_pointer_call_copy;
5256
+ assign_ast_target = assign_ast_target_copy;
5265
5257
if (tmp) {
5266
5258
// This happens if `m.m_value` is `empty`, such as in:
5267
5259
// a = empty(16)
@@ -6675,10 +6667,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
6675
6667
AST::Name_t *n = AST::down_cast<AST::Name_t>(c->m_func );
6676
6668
call_name = n->m_id ;
6677
6669
ASR::symbol_t * s = current_scope->resolve_symbol (call_name);
6678
- if ( call_name == " c_p_pointer" && !s ) {
6679
- tmp = create_CPtrToPointer (*c);
6680
- return ;
6681
- }
6682
6670
if ( call_name == " p_c_pointer" && !s ) {
6683
6671
tmp = create_PointerToCPtr (*c);
6684
6672
return ;
@@ -6793,45 +6781,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
6793
6781
}
6794
6782
}
6795
6783
6796
-
6797
- ASR::asr_t * create_CPtrToPointer (const AST::Call_t& x) {
6798
- if ( x.n_args != 2 && x.n_args != 3 ) {
6799
- throw SemanticError (" c_p_pointer accepts maximum three positional arguments, "
6800
- " first a variable of c_ptr type, second "
6801
- " the target type of the first variable and "
6802
- " third optionally the shape of the target variable "
6803
- " if target variable is an array" ,
6804
- x.base .base .loc );
6805
- }
6806
- visit_expr (*x.m_args [0 ]);
6807
- ASR::expr_t * cptr = ASRUtils::EXPR (tmp);
6808
- visit_expr (*x.m_args [1 ]);
6809
- ASR::expr_t * pptr = ASRUtils::EXPR (tmp);
6810
- ASR::expr_t * target_shape = nullptr ;
6811
- if ( x.n_args == 3 ) {
6812
- visit_expr (*x.m_args [2 ]);
6813
- target_shape = ASRUtils::EXPR (tmp);
6814
- }
6815
- bool is_allocatable = false ;
6816
- ASR::ttype_t * asr_alloc_type = ast_expr_to_asr_type (x.m_args [1 ]->base .loc , *x.m_args [1 ], is_allocatable);
6817
- ASR::ttype_t * target_type = ASRUtils::type_get_past_pointer (ASRUtils::expr_type (pptr));
6818
- if ( !ASRUtils::types_equal (target_type, asr_alloc_type, true ) ) {
6819
- diag.add (diag::Diagnostic (
6820
- " Type mismatch in c_p_pointer and target variable, the types must match" ,
6821
- diag::Level::Error, diag::Stage::Semantic, {
6822
- diag::Label (" type mismatch ('" + ASRUtils::type_to_str_python (target_type) +
6823
- " ' and '" + ASRUtils::type_to_str_python (asr_alloc_type) + " ')" ,
6824
- {target_type->base .loc , asr_alloc_type->base .loc })
6825
- })
6826
- );
6827
- throw SemanticAbort ();
6828
- }
6829
- const Location& loc = x.base .base .loc ;
6830
- fill_shape_and_lower_bound_for_CPtrToPointer ();
6831
- return ASR::make_CPtrToPointer_t (al, loc, cptr,
6832
- pptr, target_shape, lower_bounds);
6833
- }
6834
-
6835
6784
ASR::asr_t * create_PointerToCPtr (const AST::Call_t& x) {
6836
6785
if ( x.n_args != 2 ) {
6837
6786
throw SemanticError (" p_c_pointer accepts two positional arguments, "
0 commit comments