Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ macro(RUN)
endif()

add_executable(${name} ${name}.c ${RUN_EXTRAFILES})
target_include_directories(${name} PRIVATE ${CMAKE_SOURCE_DIR})
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE C)
target_link_libraries(${name} lpython_rtlib)
add_test(${name} ${CMAKE_CURRENT_BINARY_DIR}/${name})
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/bindc_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
class ArrayWrapped:
array: CPtr

@ccall
@ccall(header="bindc_03b.h")
def g(a: CPtr, value: i32, offset_value: bool) -> None:
pass

def gpy(a: CPtr, value: i32, offset_value: bool) -> None:
g(a, value, offset_value)

@ccall
@ccall(header="bindc_03b.h")
def get_array(size: i32) -> CPtr:
pass

Expand Down
4 changes: 2 additions & 2 deletions integration_tests/bindc_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
class Void:
data: CPtr

@ccall
@ccall(header="bindc_05b.h")
def trunc_custom(value: Pointer[CPtr]) -> CPtr:
pass

@ccall
@ccall(header="bindc_05b.h")
def print_value(value: CPtr):
pass

Expand Down
5 changes: 5 additions & 0 deletions integration_tests/bindc_05b.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
#ifndef BINDC_05BH
#define BINDC_05BH

void* trunc_custom(void** value);
void print_value(float* value);

#endif // BINDC_05BH
2 changes: 1 addition & 1 deletion integration_tests/bindc_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CompareOperator:
op_code: i32
op_name: str

@ccall
@ccall(header="bindc_06b.h")
def compare_array_element(value1: i32, value2: f64, op: i32) -> i32:
pass

Expand Down
5 changes: 5 additions & 0 deletions integration_tests/bindc_06b.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#ifndef BINDC_06BH
#define BINDC_06BH

#include <inttypes.h>

int32_t compare_array_element(int32_t value1, double value2, int32_t code);

#endif // BINDC_06BH
2 changes: 1 addition & 1 deletion src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ symbol
bool loaded_from_mod, bool intrinsic)
| Function(symbol_table symtab, identifier name, ttype function_signature,
identifier* dependencies, expr* args, stmt* body, expr? return_var,
access access, bool deterministic, bool side_effect_free)
access access, bool deterministic, bool side_effect_free, string? c_header)
| GenericProcedure(symbol_table parent_symtab, identifier name,
symbol* procs, access access)
| CustomOperator(symbol_table parent_symtab, identifier name,
Expand Down
4 changes: 2 additions & 2 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2660,7 +2660,7 @@ inline ASR::asr_t* make_Function_t_util(Allocator& al, const Location& loc,
ASR::deftypeType m_deftype, char* m_bindc_name, bool m_elemental, bool m_pure,
bool m_module, bool m_inline, bool m_static, ASR::ttype_t** m_type_params,
size_t n_type_params, ASR::symbol_t** m_restrictions, size_t n_restrictions,
bool m_is_restriction, bool m_deterministic, bool m_side_effect_free) {
bool m_is_restriction, bool m_deterministic, bool m_side_effect_free, char *m_c_header=nullptr) {
Vec<ASR::ttype_t*> arg_types;
arg_types.reserve(al, n_args);
for( size_t i = 0; i < n_args; i++ ) {
Expand All @@ -2678,7 +2678,7 @@ inline ASR::asr_t* make_Function_t_util(Allocator& al, const Location& loc,
return ASR::make_Function_t(
al, loc, m_symtab, m_name, func_type, m_dependencies, n_dependencies,
a_args, n_args, m_body, n_body, m_return_var, m_access, m_deterministic,
m_side_effect_free);
m_side_effect_free, m_c_header);
}

class ExprStmtDuplicator: public ASR::BaseExprStmtDuplicator<ExprStmtDuplicator>
Expand Down
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
38 changes: 34 additions & 4 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3483,7 +3483,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
bool is_restriction = false;
bool is_deterministic = false;
bool is_side_effect_free = false;

char *bindc_name=nullptr, *c_header_file=nullptr;
if (x.n_decorator_list > 0) {
for(size_t i=0; i<x.n_decorator_list; i++) {
AST::expr_t *dec = x.m_decorator_list[i];
Expand Down Expand Up @@ -3512,6 +3512,35 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
throw SemanticError("Decorator: " + name + " is not supported",
x.base.base.loc);
}
} else if (AST::is_a<AST::Call_t>(*dec)) {
AST::Call_t *call_d = AST::down_cast<AST::Call_t>(dec);
if (AST::is_a<AST::Name_t>(*call_d->m_func)) {
std::string name = AST::down_cast<AST::Name_t>(call_d->m_func)->m_id;
if (name == "ccall") {
current_procedure_abi_type = ASR::abiType::BindC;
current_procedure_interface = true;
if (call_d->n_keywords > 0) {
for (size_t i=0; i < call_d->n_keywords; i++) {
if (std::string(call_d->m_keywords[i].m_arg) == "header") {
if (AST::is_a<AST::ConstantStr_t>(*call_d->m_keywords[i].m_value)) {
std::string header_name = AST::down_cast<AST::ConstantStr_t>(
call_d->m_keywords[i].m_value)->m_value;
c_header_file = s2c(al, header_name);
} else {
throw SemanticError("header should be constant string in ccall",
x.base.base.loc);
}
}
}
}
} else {
throw SemanticError("Unsupported Decorator type",
x.base.base.loc);
}
} else {
throw SemanticError("Only Name is supported in Call decorators for now",
x.base.base.loc);
}
} else {
throw SemanticError("Unsupported Decorator type",
x.base.base.loc);
Expand Down Expand Up @@ -3612,7 +3641,6 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
current_procedure_interface) {
deftype = ASR::deftypeType::Interface;
}
char *bindc_name=nullptr;
if (x.m_returns && !AST::is_a<AST::ConstantNone_t>(*x.m_returns)) {
if (AST::is_a<AST::Name_t>(*x.m_returns) || AST::is_a<AST::Subscript_t>(*x.m_returns)) {
std::string return_var_name = "_lpython_return_variable";
Expand Down Expand Up @@ -3648,7 +3676,8 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
/* a_return_var */ ASRUtils::EXPR(return_var_ref),
current_procedure_abi_type,
s_access, deftype, bindc_name, vectorize, false, false, is_inline, is_static,
tps.p, tps.size(), nullptr, 0, is_restriction, is_deterministic, is_side_effect_free);
tps.p, tps.size(), nullptr, 0, is_restriction, is_deterministic, is_side_effect_free,
c_header_file);
return_variable->m_type = return_type_;
} else {
throw SemanticError("Return variable must be an identifier (Name AST node) or an array (Subscript AST node)",
Expand All @@ -3669,7 +3698,8 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
current_procedure_abi_type,
s_access, deftype, bindc_name,
false, is_pure, is_module, is_inline, is_static,
tps.p, tps.size(), nullptr, 0, is_restriction, is_deterministic, is_side_effect_free);
tps.p, tps.size(), nullptr, 0, is_restriction, is_deterministic, is_side_effect_free,
c_header_file);
}
ASR::symbol_t * t = ASR::down_cast<ASR::symbol_t>(tmp);
parent_scope->add_symbol(sym_name, t);
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-f955627.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-f955627.stdout",
"stdout_hash": "c446111843674f99b678a4719a280095d6dcd5426250fd2634b3a1ef",
"stdout_hash": "ae255051c7b45506791318e252b42358043c41c3f3c13848d11e91b8",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading