Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: ASR: Handle c_p_pointer() in visit_Call() #2302

Merged
merged 2 commits into from
Aug 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
ASR: Handle c_p_pointer() in visit_Call()
Thus remove the is_c_p_pointer_call usage hopefully making code clean
  • Loading branch information
Shaikh-Ubaid committed Aug 26, 2023
commit 4c888a141be2c744fad25cd03ae73e75a2afa9b6
54 changes: 17 additions & 37 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
Vec<ASR::stmt_t*> *current_body;
ASR::ttype_t* ann_assign_target_type;
AST::expr_t* assign_ast_target;
bool is_c_p_pointer_call;

std::map<std::string, int> generic_func_nums;
std::map<std::string, std::map<std::string, ASR::ttype_t*>> generic_func_subs;
Expand Down Expand Up @@ -535,7 +534,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
: diag{diagnostics}, al{al}, lm{lm}, current_scope{symbol_table}, main_module{main_module}, module_name{module_name},
ast_overload{ast_overload}, parent_dir{parent_dir}, import_paths{import_paths},
current_body{nullptr}, ann_assign_target_type{nullptr},
assign_ast_target{nullptr}, is_c_p_pointer_call{false}, allow_implicit_casting{allow_implicit_casting_} {
assign_ast_target{nullptr}, allow_implicit_casting{allow_implicit_casting_} {
current_module_dependencies.reserve(al, 4);
global_init.reserve(al, 1);
}
Expand Down Expand Up @@ -3023,14 +3022,12 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
type = ASRUtils::TYPE(ASR::make_Allocatable_t(al, type->base.loc,
ASRUtils::type_get_past_pointer(type)));
}
bool is_c_p_pointer_call_copy = is_c_p_pointer_call;
ASR::expr_t *value = nullptr;

create_add_variable_to_scope(var_name, type,
Copy link
Collaborator Author

@Shaikh-Ubaid Shaikh-Ubaid Aug 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The c_p_pointer() needs the variable to which it is being assigned. Therefore, in this PR, we first add the variable to scope, then visit the value/c_p_pointer() and later set the variable's initial value (init_val) in process_variable_init_val().

x.base.base.loc, abi, storage_type);

if( !init_expr ) {
tmp = nullptr;
is_c_p_pointer_call = false;
if (x.m_value) {
this->visit_expr(*x.m_value);
} else {
Expand All @@ -3040,13 +3037,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
ASRUtils::type_to_str_python(type), x.base.base.loc);
}
}
if( is_c_p_pointer_call ) {
create_add_variable_to_scope(var_name, nullptr, type,
x.base.base.loc, abi, storage_type);
AST::Call_t* c_p_pointer_call = AST::down_cast<AST::Call_t>(x.m_value);
tmp = create_CPtrToPointer(*c_p_pointer_call);
} else if (tmp) {
value = ASRUtils::EXPR(tmp);
if (tmp && ASR::is_a<ASR::expr_t>(*tmp)) {
ASR::expr_t* value = ASRUtils::EXPR(tmp);
ASR::ttype_t* underlying_type = type;
if( ASR::is_a<ASR::Const_t>(*type) ) {
underlying_type = ASRUtils::get_contained_type(type);
Expand All @@ -3070,10 +3062,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
cast_helper(type, init_expr, init_expr->base.loc);
}

if( !is_c_p_pointer_call ) {
if (!inside_struct || ASR::is_a<ASR::Const_t>(*type)) {
process_variable_init_val(current_scope->get_symbol(var_name), x.base.base.loc, init_expr);
}
if (!inside_struct || ASR::is_a<ASR::Const_t>(*type)) {
process_variable_init_val(current_scope->get_symbol(var_name), x.base.base.loc, init_expr);
}

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

if( !is_c_p_pointer_call ) {
if ( !(tmp && ASR::is_a<ASR::stmt_t>(*tmp) &&
ASR::is_a<ASR::CPtrToPointer_t>(*ASR::down_cast<ASR::stmt_t>(tmp))) ) {
tmp = nullptr;
}
is_c_p_pointer_call = is_c_p_pointer_call_copy;
ann_assign_target_type = ann_assign_target_type_copy;
}

Expand Down Expand Up @@ -5253,18 +5243,13 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
ASR::expr_t *target, *assign_value = nullptr, *tmp_value;
AST::expr_t* assign_ast_target_copy = assign_ast_target;
assign_ast_target = x.m_targets[0];
bool is_c_p_pointer_call_copy = is_c_p_pointer_call;
is_c_p_pointer_call = false;
this->visit_expr(*x.m_value);
if( is_c_p_pointer_call ) {
LCOMPILERS_ASSERT(x.n_targets == 1);
AST::Call_t* c_p_pointer_call = AST::down_cast<AST::Call_t>(x.m_value);
tmp = create_CPtrToPointer(*c_p_pointer_call);
return ;
}
is_c_p_pointer_call = is_c_p_pointer_call_copy;
assign_ast_target = assign_ast_target_copy;
if (tmp) {
if (ASR::is_a<ASR::stmt_t>(*tmp)) {
// This happens for c_p_pointer()
return;
}
// This happens if `m.m_value` is `empty`, such as in:
// a = empty(16)
// We skip this statement for now, the array is declared
Expand Down Expand Up @@ -7472,20 +7457,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
if (AST::is_a<AST::Name_t>(*x.m_func)) {
AST::Name_t *n = AST::down_cast<AST::Name_t>(x.m_func);
call_name = n->m_id;
}
if (call_name == "c_p_pointer" &&
!current_scope->resolve_symbol(call_name)) {
is_c_p_pointer_call = true;
tmp = nullptr;
return ;
}

if (AST::is_a<AST::Attribute_t>(*x.m_func)) {
} else if (AST::is_a<AST::Attribute_t>(*x.m_func)) {
parse_args(x, args);
AST::Attribute_t *at = AST::down_cast<AST::Attribute_t>(x.m_func);
handle_attribute(at, args, x.base.base.loc);
return;
} else if( call_name == "" ) {
} else {
throw SemanticError("Only Name or Attribute type supported in Call",
x.base.base.loc);
}
Expand Down Expand Up @@ -7575,6 +7552,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
// with the type
tmp = nullptr;
return;
} else if (call_name == "c_p_pointer") {
tmp = create_CPtrToPointer(x);
return;
} else if (call_name == "empty_c_void_p") {
// TODO: check that `empty_c_void_p uses` has arguments that are compatible
// with the type
Expand Down