Skip to content

Commit 456b616

Browse files
committed
ASR: Merge create_CPtrToPointerFromArgs() and create_CPtrToPointer()
1 parent 615e7e7 commit 456b616

File tree

1 file changed

+24
-75
lines changed

1 file changed

+24
-75
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 24 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2793,27 +2793,33 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
27932793
ASR::arraystorageType::RowMajor)); \
27942794
} \
27952795

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]);
27992806
ASR::expr_t* cptr = ASRUtils::EXPR(tmp);
2800-
this->visit_expr(*ast_pptr);
2807+
this->visit_expr(*assign_ast_target);
28012808
ASR::expr_t* pptr = ASRUtils::EXPR(tmp);
28022809
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]);
28052812
target_shape = ASRUtils::EXPR(tmp);
28062813
}
28072814
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);
28102816
ASR::ttype_t* target_type = ASRUtils::type_get_past_pointer(ASRUtils::expr_type(pptr));
28112817
if( !ASRUtils::types_equal(target_type, asr_alloc_type, true) ) {
28122818
diag.add(diag::Diagnostic(
28132819
"Type mismatch in c_p_pointer and target variable, the types must match",
28142820
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) + "')",
28172823
{target_type->base.loc, asr_alloc_type->base.loc})
28182824
})
28192825
);
@@ -2837,8 +2843,10 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
28372843
}
28382844
}
28392845
}
2846+
const Location& loc = x.base.base.loc;
28402847
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);
28422850
}
28432851

28442852
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> {
30223030
create_add_variable_to_scope(var_name, nullptr, type,
30233031
x.base.base.loc, abi, storage_type);
30243032
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);
30373034
} else if (tmp) {
30383035
value = ASRUtils::EXPR(tmp);
30393036
ASR::ttype_t* underlying_type = type;
@@ -5244,24 +5241,19 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
52445241

52455242
void visit_Assign(const AST::Assign_t &x) {
52465243
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];
52475246
bool is_c_p_pointer_call_copy = is_c_p_pointer_call;
52485247
is_c_p_pointer_call = false;
52495248
this->visit_expr(*x.m_value);
52505249
if( is_c_p_pointer_call ) {
52515250
LCOMPILERS_ASSERT(x.n_targets == 1);
52525251
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);
52625253
return ;
52635254
}
52645255
is_c_p_pointer_call = is_c_p_pointer_call_copy;
5256+
assign_ast_target = assign_ast_target_copy;
52655257
if (tmp) {
52665258
// This happens if `m.m_value` is `empty`, such as in:
52675259
// a = empty(16)
@@ -6675,10 +6667,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
66756667
AST::Name_t *n = AST::down_cast<AST::Name_t>(c->m_func);
66766668
call_name = n->m_id;
66776669
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-
}
66826670
if( call_name == "p_c_pointer" && !s ) {
66836671
tmp = create_PointerToCPtr(*c);
66846672
return;
@@ -6793,45 +6781,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
67936781
}
67946782
}
67956783

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-
68356784
ASR::asr_t* create_PointerToCPtr(const AST::Call_t& x) {
68366785
if( x.n_args != 2 ) {
68376786
throw SemanticError("p_c_pointer accepts two positional arguments, "

0 commit comments

Comments
 (0)