Skip to content

Commit 4c888a1

Browse files
committed
ASR: Handle c_p_pointer() in visit_Call()
Thus remove the is_c_p_pointer_call usage hopefully making code clean
1 parent 075aa66 commit 4c888a1

File tree

1 file changed

+17
-37
lines changed

1 file changed

+17
-37
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,6 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
498498
Vec<ASR::stmt_t*> *current_body;
499499
ASR::ttype_t* ann_assign_target_type;
500500
AST::expr_t* assign_ast_target;
501-
bool is_c_p_pointer_call;
502501

503502
std::map<std::string, int> generic_func_nums;
504503
std::map<std::string, std::map<std::string, ASR::ttype_t*>> generic_func_subs;
@@ -535,7 +534,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
535534
: diag{diagnostics}, al{al}, lm{lm}, current_scope{symbol_table}, main_module{main_module}, module_name{module_name},
536535
ast_overload{ast_overload}, parent_dir{parent_dir}, import_paths{import_paths},
537536
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_} {
539538
current_module_dependencies.reserve(al, 4);
540539
global_init.reserve(al, 1);
541540
}
@@ -3023,14 +3022,12 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
30233022
type = ASRUtils::TYPE(ASR::make_Allocatable_t(al, type->base.loc,
30243023
ASRUtils::type_get_past_pointer(type)));
30253024
}
3026-
bool is_c_p_pointer_call_copy = is_c_p_pointer_call;
3027-
ASR::expr_t *value = nullptr;
3025+
30283026
create_add_variable_to_scope(var_name, type,
30293027
x.base.base.loc, abi, storage_type);
30303028

30313029
if( !init_expr ) {
30323030
tmp = nullptr;
3033-
is_c_p_pointer_call = false;
30343031
if (x.m_value) {
30353032
this->visit_expr(*x.m_value);
30363033
} else {
@@ -3040,13 +3037,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
30403037
ASRUtils::type_to_str_python(type), x.base.base.loc);
30413038
}
30423039
}
3043-
if( is_c_p_pointer_call ) {
3044-
create_add_variable_to_scope(var_name, nullptr, type,
3045-
x.base.base.loc, abi, storage_type);
3046-
AST::Call_t* c_p_pointer_call = AST::down_cast<AST::Call_t>(x.m_value);
3047-
tmp = create_CPtrToPointer(*c_p_pointer_call);
3048-
} else if (tmp) {
3049-
value = ASRUtils::EXPR(tmp);
3040+
if (tmp && ASR::is_a<ASR::expr_t>(*tmp)) {
3041+
ASR::expr_t* value = ASRUtils::EXPR(tmp);
30503042
ASR::ttype_t* underlying_type = type;
30513043
if( ASR::is_a<ASR::Const_t>(*type) ) {
30523044
underlying_type = ASRUtils::get_contained_type(type);
@@ -3070,10 +3062,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
30703062
cast_helper(type, init_expr, init_expr->base.loc);
30713063
}
30723064

3073-
if( !is_c_p_pointer_call ) {
3074-
if (!inside_struct || ASR::is_a<ASR::Const_t>(*type)) {
3075-
process_variable_init_val(current_scope->get_symbol(var_name), x.base.base.loc, init_expr);
3076-
}
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);
30773067
}
30783068

30793069
if (is_allocatable && x.m_value && AST::is_a<AST::Call_t>(*x.m_value)) {
@@ -3083,10 +3073,10 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
30833073
}
30843074
}
30853075

3086-
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))) ) {
30873078
tmp = nullptr;
30883079
}
3089-
is_c_p_pointer_call = is_c_p_pointer_call_copy;
30903080
ann_assign_target_type = ann_assign_target_type_copy;
30913081
}
30923082

@@ -5253,18 +5243,13 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
52535243
ASR::expr_t *target, *assign_value = nullptr, *tmp_value;
52545244
AST::expr_t* assign_ast_target_copy = assign_ast_target;
52555245
assign_ast_target = x.m_targets[0];
5256-
bool is_c_p_pointer_call_copy = is_c_p_pointer_call;
5257-
is_c_p_pointer_call = false;
52585246
this->visit_expr(*x.m_value);
5259-
if( is_c_p_pointer_call ) {
5260-
LCOMPILERS_ASSERT(x.n_targets == 1);
5261-
AST::Call_t* c_p_pointer_call = AST::down_cast<AST::Call_t>(x.m_value);
5262-
tmp = create_CPtrToPointer(*c_p_pointer_call);
5263-
return ;
5264-
}
5265-
is_c_p_pointer_call = is_c_p_pointer_call_copy;
52665247
assign_ast_target = assign_ast_target_copy;
52675248
if (tmp) {
5249+
if (ASR::is_a<ASR::stmt_t>(*tmp)) {
5250+
// This happens for c_p_pointer()
5251+
return;
5252+
}
52685253
// This happens if `m.m_value` is `empty`, such as in:
52695254
// a = empty(16)
52705255
// We skip this statement for now, the array is declared
@@ -7472,20 +7457,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
74727457
if (AST::is_a<AST::Name_t>(*x.m_func)) {
74737458
AST::Name_t *n = AST::down_cast<AST::Name_t>(x.m_func);
74747459
call_name = n->m_id;
7475-
}
7476-
if (call_name == "c_p_pointer" &&
7477-
!current_scope->resolve_symbol(call_name)) {
7478-
is_c_p_pointer_call = true;
7479-
tmp = nullptr;
7480-
return ;
7481-
}
7482-
7483-
if (AST::is_a<AST::Attribute_t>(*x.m_func)) {
7460+
} else if (AST::is_a<AST::Attribute_t>(*x.m_func)) {
74847461
parse_args(x, args);
74857462
AST::Attribute_t *at = AST::down_cast<AST::Attribute_t>(x.m_func);
74867463
handle_attribute(at, args, x.base.base.loc);
74877464
return;
7488-
} else if( call_name == "" ) {
7465+
} else {
74897466
throw SemanticError("Only Name or Attribute type supported in Call",
74907467
x.base.base.loc);
74917468
}
@@ -7575,6 +7552,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
75757552
// with the type
75767553
tmp = nullptr;
75777554
return;
7555+
} else if (call_name == "c_p_pointer") {
7556+
tmp = create_CPtrToPointer(x);
7557+
return;
75787558
} else if (call_name == "empty_c_void_p") {
75797559
// TODO: check that `empty_c_void_p uses` has arguments that are compatible
75807560
// with the type

0 commit comments

Comments
 (0)