Skip to content

Commit

Permalink
Refactor: Handle type conv inside get_py_obj_return_type_conv_func_fr…
Browse files Browse the repository at this point in the history
…om_ttype_t()
  • Loading branch information
Shaikh-Ubaid committed Jun 23, 2023
1 parent 84fbd34 commit e3663a0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
30 changes: 6 additions & 24 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,31 +539,13 @@ R"(#include <stdio.h>
if (!x.m_return_var) return "";
ASR::Variable_t* r_v = ASRUtils::EXPR2VAR(x.m_return_var);
std::string indent = "\n ";
std::string py_val_cnvrt, ret_var_decl, copy_result;
if (ASRUtils::is_aggregate_type(r_v->m_type)) {
if (ASRUtils::is_array(r_v->m_type)) {
ASR::ttype_t* array_type_asr = ASRUtils::type_get_past_array(r_v->m_type);
std::string array_type_name = CUtils::get_c_type_from_ttype_t(array_type_asr);
std::string array_encoded_type_name = ASRUtils::get_type_code(array_type_asr, true, false);
std::string return_type = c_ds_api->get_array_type(array_type_name, array_encoded_type_name, array_types_decls, true);
py_val_cnvrt = bind_py_utils_functions->get_conv_py_arr_to_c(return_type, array_type_name,
array_encoded_type_name) + "(pValue)";
ret_var_decl = indent + return_type + " _lpython_return_variable;";
} else {
if (ASRUtils::is_character(*r_v->m_type)) {
py_val_cnvrt = CUtils::get_py_obj_return_type_conv_func_from_ttype_t(r_v->m_type) + "(pValue)";
ret_var_decl = indent + CUtils::get_c_type_from_ttype_t(r_v->m_type) + " _lpython_return_variable;";
copy_result = indent + "_lpython_return_variable = _lfortran_str_copy(" + std::string(r_v->m_name) + ", 1, 0);";
}
}
} else {
py_val_cnvrt = CUtils::get_py_obj_return_type_conv_func_from_ttype_t(r_v->m_type) + "(pValue)";
ret_var_decl = indent + CUtils::get_c_type_from_ttype_t(r_v->m_type) + " _lpython_return_variable;";
}
std::string ret_assign = indent + std::string(r_v->m_name) + " = " + py_val_cnvrt + ";";
std::string ret_stmt = indent + "return _lpython_return_variable;";
std::string ret_var_decl = indent + CUtils::get_c_type_from_ttype_t(r_v->m_type) + " _lpython_return_variable;";
std::string py_val_cnvrt = CUtils::get_py_obj_return_type_conv_func_from_ttype_t(r_v->m_type,
array_types_decls, c_ds_api, bind_py_utils_functions);
std::string ret_assign = indent + "_lpython_return_variable = " + py_val_cnvrt + "(pValue);";
std::string clear_pValue = indent + "Py_DECREF(pValue);";
return ret_var_decl + ret_assign + copy_result + clear_pValue + ret_stmt + "\n";
std::string ret_stmt = indent + "return _lpython_return_variable;";
return ret_var_decl + ret_assign + clear_pValue + ret_stmt + "\n";
}

std::string get_func_body_bind_python(const ASR::Function_t &x) {
Expand Down
42 changes: 32 additions & 10 deletions src/libasr/codegen/c_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,28 @@ namespace CUtils {
conv_py_arr_to_c(return_type, element_type, encoded_type);
return util2func["conv_py_arr_to_c_" + encoded_type];
}

void conv_py_str_to_c() {
if( util2func.find("conv_py_str_to_c") != util2func.end() ) {
return;
}
std::string indent(indentation_level * indentation_spaces, ' ');
std::string tab(indentation_spaces, ' ');
util2func["conv_py_str_to_c"] = global_scope->get_unique_name("conv_py_str_to_c");
std::string conv_py_arr_to_c_func = util2func["conv_py_str_to_c"];
std::string signature = "static inline char* " + conv_py_arr_to_c_func + "(PyObject* pValue)";
util_func_decls += indent + signature + ";\n";
std::string body = indent + signature + " {\n";
body += indent + tab + "char *s = (char*)PyUnicode_AsUTF8(pValue);\n";
body += indent + tab + "return _lfortran_str_copy(s, 1, 0);\n";
body += indent + "}\n\n";
util_funcs += body;
}

std::string get_conv_py_str_to_c() {
conv_py_str_to_c();
return util2func["conv_py_str_to_c"];
}
};

static inline std::string get_tuple_type_code(ASR::Tuple_t *tup) {
Expand Down Expand Up @@ -523,19 +545,19 @@ namespace CUtils {
return type_src;
}

static inline std::string get_py_obj_return_type_conv_func_from_ttype_t(ASR::ttype_t* t) {
static inline std::string get_py_obj_return_type_conv_func_from_ttype_t(ASR::ttype_t* t,
std::string &array_types_decls, std::unique_ptr<CCPPDSUtils> &c_ds_api,
std::unique_ptr<CUtils::BindPyUtilFunctions> &bind_py_utils_functions) {
int kind = ASRUtils::extract_kind_from_ttype_t(t);
std::string type_src = "";
switch( t->type ) {
case ASR::ttypeType::Array: {
ASR::ttype_t* arr_type = ASR::down_cast<ASR::Array_t>(t)->m_type;
if (arr_type->type == ASR::ttypeType::Integer) {
type_src = ""; // we convert the array while copying it
} else if (arr_type->type == ASR::ttypeType::Real) {
type_src = ""; // we convert the array while copying it
} else {
throw CodeGenError("get_py_obj_return_type_conv_func_from_ttype_t: Unsupported array type for return variable");
}
ASR::ttype_t* array_t = ASR::down_cast<ASR::Array_t>(t)->m_type;
std::string array_type_name = CUtils::get_c_type_from_ttype_t(array_t);
std::string array_encoded_type_name = ASRUtils::get_type_code(array_t, true, false);
std::string return_type = c_ds_api->get_array_type(array_type_name, array_encoded_type_name, array_types_decls, true);
type_src = bind_py_utils_functions->get_conv_py_arr_to_c(return_type, array_type_name,
array_encoded_type_name);
break;
}
case ASR::ttypeType::Integer: {
Expand Down Expand Up @@ -563,7 +585,7 @@ namespace CUtils {
break;
}
case ASR::ttypeType::Character: {
type_src = "(char*)PyUnicode_AsUTF8";
type_src = bind_py_utils_functions->get_conv_py_str_to_c();
break;
}
default: {
Expand Down

0 comments on commit e3663a0

Please sign in to comment.