Skip to content
Merged
Show file tree
Hide file tree
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
Next Next commit
C: Recognize ccall header argument
  • Loading branch information
Smit-create committed Apr 26, 2023
commit 046d601900a3e7b48901642083a75e7208046166
15 changes: 9 additions & 6 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
std::string dims;
use_ref = use_ref && !is_array;
if (ASRUtils::is_integer(*v_m_type)) {
headers.insert("inttypes");
headers.insert("inttypes.h");
ASR::Integer_t *t = ASR::down_cast<ASR::Integer_t>(v_m_type);
std::string type_name = "int" + std::to_string(t->m_kind * 8) + "_t";
if( is_array ) {
Expand Down Expand Up @@ -354,7 +354,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
sub = format_type_c(dims, type_name, v_m_name, use_ref, dummy);
}
} else if (ASRUtils::is_complex(*v_m_type)) {
headers.insert("complex");
headers.insert("complex.h");
ASR::Complex_t *t = ASR::down_cast<ASR::Complex_t>(v_m_type);
std::string type_name = "float complex";
if (t->m_kind == 8) type_name = "double complex";
Expand Down Expand Up @@ -713,8 +713,11 @@ R"(
}
}
std::string to_include = "";
for (auto s: headers) {
to_include += "#include <" + s + ".h>\n";
for (auto &s: headers) {
to_include += "#include <" + s + ">\n";
}
for (auto &s: user_headers) {
to_include += "#include \"" + s + "\"\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

For now let's use <, that is closer to correct.

We probably should distinguish between a header file that should be referenced with < (intrinsic ones) and " (user ones).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done, thanks!

}
if( c_ds_api->get_func_decls().size() > 0 ) {
array_types_decls += "\n" + c_ds_api->get_func_decls() + "\n";
Expand Down Expand Up @@ -1006,7 +1009,7 @@ R"(
}

void visit_ComplexConstant(const ASR::ComplexConstant_t &x) {
headers.insert("complex");
headers.insert("complex.h");
std::string re = std::to_string(x.m_re);
std::string im = std::to_string(x.m_im);
src = "CMPLX(" + re + ", " + im + ")";
Expand Down Expand Up @@ -1203,7 +1206,7 @@ R"(

void visit_ArrayConstant(const ASR::ArrayConstant_t& x) {
// TODO: Support and test for multi-dimensional array constants
headers.insert("stdarg");
headers.insert("stdarg.h");
std::string array_const = "";
for( size_t i = 0; i < x.n_args; i++ ) {
visit_expr(*x.m_args[i]);
Expand Down
27 changes: 17 additions & 10 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
// Use std::complex<float/double> or float/double complex
bool gen_stdcomplex;
bool is_c;
std::set<std::string> headers;
std::set<std::string> headers, user_headers;
std::vector<std::string> tmp_buffer_src;

SymbolTable* global_scope;
Expand Down Expand Up @@ -542,9 +542,16 @@ R"(#include <stdio.h>
src = "";
return;
}
if (ASRUtils::get_FunctionType(x)->m_abi == ASR::abiType::BindC
&& ASRUtils::get_FunctionType(x)->m_deftype == ASR::deftypeType::Interface) {
sub += ";\n";
ASR::FunctionType_t *f_type = ASRUtils::get_FunctionType(x);
if (f_type->m_abi == ASR::abiType::BindC
&& f_type->m_deftype == ASR::deftypeType::Interface) {
if (x.m_c_header) {
user_headers.insert(std::string(x.m_c_header));
src = "";
return;
} else {
sub += ";\n";
}
} else {
sub += "\n";

Expand Down Expand Up @@ -1368,7 +1375,7 @@ R"(#include <stdio.h>
}
case (ASR::cast_kindType::IntegerToComplex) : {
if (is_c) {
headers.insert("complex");
headers.insert("complex.h");
src = "CMPLX(" + src + ", 0)";
} else {
src = "std::complex<double>(" + src + ")";
Expand All @@ -1378,7 +1385,7 @@ R"(#include <stdio.h>
}
case (ASR::cast_kindType::ComplexToReal) : {
if (is_c) {
headers.insert("complex");
headers.insert("complex.h");
src = "creal(" + src + ")";
} else {
src = "std::real(" + src + ")";
Expand All @@ -1388,7 +1395,7 @@ R"(#include <stdio.h>
}
case (ASR::cast_kindType::RealToComplex) : {
if (is_c) {
headers.insert("complex");
headers.insert("complex.h");
src = "CMPLX(" + src + ", 0.0)";
} else {
src = "std::complex<double>(" + src + ")";
Expand Down Expand Up @@ -1593,7 +1600,7 @@ R"(#include <stdio.h>
}

void visit_ComplexRe(const ASR::ComplexRe_t &x) {
headers.insert("complex");
headers.insert("complex.h");
CHECK_FAST_C_CPP(compiler_options, x)
self().visit_expr(*x.m_arg);
if (is_c) {
Expand All @@ -1604,7 +1611,7 @@ R"(#include <stdio.h>
}

void visit_ComplexIm(const ASR::ComplexIm_t &x) {
headers.insert("complex");
headers.insert("complex.h");
CHECK_FAST_C_CPP(compiler_options, x)
self().visit_expr(*x.m_arg);
if (is_c) {
Expand Down Expand Up @@ -1683,7 +1690,7 @@ R"(#include <stdio.h>
case (ASR::binopType::Pow) : {
src = "pow(" + left + ", " + right + ")";
if (is_c) {
headers.insert("math");
headers.insert("math.h");
} else {
src = "std::" + src;
}
Expand Down