Skip to content

Commit d24f131

Browse files
authored
Merge pull request #2302 from Shaikh-Ubaid/asr_refactor_c_p_pointer3
Refactor: ASR: Handle c_p_pointer() in visit_Call()
2 parents a838e88 + 4c888a1 commit d24f131

File tree

1 file changed

+56
-66
lines changed

1 file changed

+56
-66
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 56 additions & 66 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
}
@@ -2674,14 +2673,22 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
26742673
return std::string(base_name->m_id) == "Union";
26752674
}
26762675

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) {
26812677
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;
26822681
if( init_expr ) {
26832682
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;
26842690
}
2691+
26852692
bool is_runtime_expression = !ASRUtils::is_value_constant(value);
26862693
bool is_variable_const = ASR::is_a<ASR::Const_t>(*type);
26872694

@@ -2690,27 +2697,6 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
26902697
" is not initialised at declaration.", loc);
26912698
}
26922699

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-
27142700
if( init_expr && (current_body || ASR::is_a<ASR::List_t>(*type) ||
27152701
is_runtime_expression) && !is_variable_const) {
27162702
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> {
27382724
current_body ) {
27392725
throw SemanticError("Initialisation of " + var_name + " must reduce to a compile time constant.", loc);
27402726
}
2727+
}
27412728

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);
27422752
current_scope->add_or_overwrite_symbol(var_name, v_sym);
27432753
}
27442754

@@ -3012,11 +3022,12 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
30123022
type = ASRUtils::TYPE(ASR::make_Allocatable_t(al, type->base.loc,
30133023
ASRUtils::type_get_past_pointer(type)));
30143024
}
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+
30173029
if( !init_expr ) {
30183030
tmp = nullptr;
3019-
is_c_p_pointer_call = false;
30203031
if (x.m_value) {
30213032
this->visit_expr(*x.m_value);
30223033
} else {
@@ -3026,13 +3037,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
30263037
ASRUtils::type_to_str_python(type), x.base.base.loc);
30273038
}
30283039
}
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);
30363042
ASR::ttype_t* underlying_type = type;
30373043
if( ASR::is_a<ASR::Const_t>(*type) ) {
30383044
underlying_type = ASRUtils::get_contained_type(type);
@@ -3056,14 +3062,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
30563062
cast_helper(type, init_expr, init_expr->base.loc);
30573063
}
30583064

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);
30673067
}
30683068

30693069
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> {
30733073
}
30743074
}
30753075

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))) ) {
30773078
tmp = nullptr;
30783079
}
3079-
is_c_p_pointer_call = is_c_p_pointer_call_copy;
30803080
ann_assign_target_type = ann_assign_target_type_copy;
30813081
}
30823082

@@ -5243,18 +5243,13 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
52435243
ASR::expr_t *target, *assign_value = nullptr, *tmp_value;
52445244
AST::expr_t* assign_ast_target_copy = assign_ast_target;
52455245
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;
52485246
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;
52565247
assign_ast_target = assign_ast_target_copy;
52575248
if (tmp) {
5249+
if (ASR::is_a<ASR::stmt_t>(*tmp)) {
5250+
// This happens for c_p_pointer()
5251+
return;
5252+
}
52585253
// This happens if `m.m_value` is `empty`, such as in:
52595254
// a = empty(16)
52605255
// We skip this statement for now, the array is declared
@@ -7462,20 +7457,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
74627457
if (AST::is_a<AST::Name_t>(*x.m_func)) {
74637458
AST::Name_t *n = AST::down_cast<AST::Name_t>(x.m_func);
74647459
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)) {
74747461
parse_args(x, args);
74757462
AST::Attribute_t *at = AST::down_cast<AST::Attribute_t>(x.m_func);
74767463
handle_attribute(at, args, x.base.base.loc);
74777464
return;
7478-
} else if( call_name == "" ) {
7465+
} else {
74797466
throw SemanticError("Only Name or Attribute type supported in Call",
74807467
x.base.base.loc);
74817468
}
@@ -7565,6 +7552,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
75657552
// with the type
75667553
tmp = nullptr;
75677554
return;
7555+
} else if (call_name == "c_p_pointer") {
7556+
tmp = create_CPtrToPointer(x);
7557+
return;
75687558
} else if (call_name == "empty_c_void_p") {
75697559
// TODO: check that `empty_c_void_p uses` has arguments that are compatible
75707560
// with the type

0 commit comments

Comments
 (0)