Skip to content

Commit 2e43145

Browse files
committed
Added support for pointers in DerivedRef and set abi type for ccallable decorator
1 parent 8ac8887 commit 2e43145

File tree

1 file changed

+55
-48
lines changed

1 file changed

+55
-48
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
19781978
if (name == "ccall") {
19791979
current_procedure_abi_type = ASR::abiType::BindC;
19801980
current_procedure_interface = true;
1981-
} else if (name == "ccallback") {
1981+
} else if (name == "ccallback" || name == "ccallable") {
19821982
current_procedure_abi_type = ASR::abiType::BindC;
19831983
} else if (name == "overload") {
19841984
overload = true;
@@ -2644,6 +2644,59 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
26442644

26452645
}
26462646

2647+
void visit_AttributeUtil(ASR::ttype_t* type, char* attr_char,
2648+
ASR::symbol_t *t, const Location& loc) {
2649+
if (ASRUtils::is_complex(*type)) {
2650+
std::string attr = attr_char;
2651+
if (attr == "imag") {
2652+
ASR::expr_t *val = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, loc, t));
2653+
int kind = ASRUtils::extract_kind_from_ttype_t(type);
2654+
ASR::ttype_t *dest_type = ASR::down_cast<ASR::ttype_t>(ASR::make_Real_t(al, loc,
2655+
kind, nullptr, 0));
2656+
tmp = ASR::make_ComplexIm_t(al, loc, val, dest_type, nullptr);
2657+
return;
2658+
} else if (attr == "real") {
2659+
ASR::expr_t *val = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, loc, t));
2660+
int kind = ASRUtils::extract_kind_from_ttype_t(type);
2661+
ASR::ttype_t *dest_type = ASR::down_cast<ASR::ttype_t>(ASR::make_Real_t(al, loc,
2662+
kind, nullptr, 0));
2663+
ASR::expr_t *value = ASR::down_cast<ASR::expr_t>(ASRUtils::make_Cast_t_value(
2664+
al, val->base.loc, val, ASR::cast_kindType::ComplexToReal, dest_type));
2665+
tmp = ASR::make_ComplexRe_t(al, loc, val, dest_type, ASRUtils::expr_value(value));
2666+
return;
2667+
} else {
2668+
throw SemanticError("'" + attr + "' is not implemented for Complex type",
2669+
loc);
2670+
}
2671+
} else if( ASR::is_a<ASR::Derived_t>(*type) ) {
2672+
ASR::Derived_t* der = ASR::down_cast<ASR::Derived_t>(type);
2673+
ASR::symbol_t* der_sym = ASRUtils::symbol_get_past_external(der->m_derived_type);
2674+
ASR::DerivedType_t* der_type = ASR::down_cast<ASR::DerivedType_t>(der_sym);
2675+
bool member_found = false;
2676+
std::string member_name = attr_char;
2677+
for( size_t i = 0; i < der_type->n_members && !member_found; i++ ) {
2678+
member_found = std::string(der_type->m_members[i]) == member_name;
2679+
}
2680+
if( !member_found ) {
2681+
throw SemanticError("No member " + member_name +
2682+
" found in " + std::string(der_type->m_name),
2683+
loc);
2684+
}
2685+
ASR::expr_t *val = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, loc, t));
2686+
ASR::symbol_t* member_sym = der_type->m_symtab->resolve_symbol(member_name);
2687+
LFORTRAN_ASSERT(ASR::is_a<ASR::Variable_t>(*member_sym));
2688+
ASR::Variable_t* member_var = ASR::down_cast<ASR::Variable_t>(member_sym);
2689+
tmp = ASR::make_DerivedRef_t(al, loc, val, member_sym,
2690+
member_var->m_type, nullptr);
2691+
} else if(ASR::is_a<ASR::Pointer_t>(*type)) {
2692+
ASR::Pointer_t* p = ASR::down_cast<ASR::Pointer_t>(type);
2693+
visit_AttributeUtil(p->m_type, attr_char, t, loc);
2694+
} else {
2695+
throw SemanticError(ASRUtils::type_to_str_python(type) + " not supported yet in Attribute.",
2696+
loc);
2697+
}
2698+
}
2699+
26472700
void visit_Attribute(const AST::Attribute_t &x) {
26482701
if (AST::is_a<AST::Name_t>(*x.m_value)) {
26492702
std::string value = AST::down_cast<AST::Name_t>(x.m_value)->m_id;
@@ -2654,53 +2707,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
26542707
}
26552708
if (ASR::is_a<ASR::Variable_t>(*t)) {
26562709
ASR::Variable_t *var = ASR::down_cast<ASR::Variable_t>(t);
2657-
if (ASRUtils::is_complex(*var->m_type)) {
2658-
std::string attr = x.m_attr;
2659-
if (attr == "imag") {
2660-
ASR::expr_t *val = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, x.base.base.loc, t));
2661-
int kind = ASRUtils::extract_kind_from_ttype_t(var->m_type);
2662-
ASR::ttype_t *dest_type = ASR::down_cast<ASR::ttype_t>(ASR::make_Real_t(al, x.base.base.loc,
2663-
kind, nullptr, 0));
2664-
tmp = ASR::make_ComplexIm_t(al, x.base.base.loc, val, dest_type, nullptr);
2665-
return;
2666-
} else if (attr == "real") {
2667-
ASR::expr_t *val = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, x.base.base.loc, t));
2668-
int kind = ASRUtils::extract_kind_from_ttype_t(var->m_type);
2669-
ASR::ttype_t *dest_type = ASR::down_cast<ASR::ttype_t>(ASR::make_Real_t(al, x.base.base.loc,
2670-
kind, nullptr, 0));
2671-
ASR::expr_t *value = ASR::down_cast<ASR::expr_t>(ASRUtils::make_Cast_t_value(
2672-
al, val->base.loc, val, ASR::cast_kindType::ComplexToReal, dest_type));
2673-
tmp = ASR::make_ComplexRe_t(al, x.base.base.loc, val, dest_type, ASRUtils::expr_value(value));
2674-
return;
2675-
} else {
2676-
throw SemanticError("'" + attr + "' is not implemented for Complex type",
2677-
x.base.base.loc);
2678-
}
2679-
2680-
} else if( ASR::is_a<ASR::Derived_t>(*var->m_type) ) {
2681-
ASR::Derived_t* der = ASR::down_cast<ASR::Derived_t>(var->m_type);
2682-
ASR::symbol_t* der_sym = ASRUtils::symbol_get_past_external(der->m_derived_type);
2683-
ASR::DerivedType_t* der_type = ASR::down_cast<ASR::DerivedType_t>(der_sym);
2684-
bool member_found = false;
2685-
std::string member_name = x.m_attr;
2686-
for( size_t i = 0; i < der_type->n_members && !member_found; i++ ) {
2687-
member_found = std::string(der_type->m_members[i]) == member_name;
2688-
}
2689-
if( !member_found ) {
2690-
throw SemanticError("No member " + member_name +
2691-
" found in " + std::string(der_type->m_name),
2692-
x.base.base.loc);
2693-
}
2694-
ASR::expr_t *val = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, x.base.base.loc, t));
2695-
ASR::symbol_t* member_sym = der_type->m_symtab->resolve_symbol(member_name);
2696-
LFORTRAN_ASSERT(ASR::is_a<ASR::Variable_t>(*member_sym));
2697-
ASR::Variable_t* member_var = ASR::down_cast<ASR::Variable_t>(member_sym);
2698-
tmp = ASR::make_DerivedRef_t(al, x.base.base.loc, val, member_sym,
2699-
member_var->m_type, nullptr);
2700-
} else {
2701-
throw SemanticError("Only Complex type supported for now in Attribute",
2702-
x.base.base.loc);
2703-
}
2710+
visit_AttributeUtil(var->m_type, x.m_attr, t, x.base.base.loc);
27042711
} else {
27052712
throw SemanticError("Only Variable type is supported for now in Attribute",
27062713
x.base.base.loc);

0 commit comments

Comments
 (0)