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
Next Next commit
Allow ccall to use custom headers
  • Loading branch information
Smit-create committed Apr 26, 2023
commit 873e5e47ce51e0a5a800fe7fde63a149e9c737fa
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
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