Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0daef38

Browse files
authored
Implement FunctionType in ASR (lcompilers#1518)
1 parent f9322ae commit 0daef38

File tree

179 files changed

+342
-289
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+342
-289
lines changed

src/libasr/ASR.asdl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,9 @@ symbol
8484
stmt* body)
8585
| Module(symbol_table symtab, identifier name, identifier* dependencies,
8686
bool loaded_from_mod, bool intrinsic)
87-
| Function(symbol_table symtab, identifier name, identifier* dependencies, expr* args, stmt* body,
88-
expr? return_var, abi abi, access access, deftype deftype,
89-
string? bindc_name, bool elemental, bool pure, bool module, bool inline,
90-
bool static, ttype* type_params, symbol* restrictions, bool is_restriction,
91-
bool deterministic, bool side_effect_free)
87+
| Function(symbol_table symtab, identifier name, ttype function_signature,
88+
identifier* dependencies, expr* args, stmt* body, expr? return_var,
89+
access access, bool deterministic, bool side_effect_free)
9290
| GenericProcedure(symbol_table parent_symtab, identifier name,
9391
symbol* procs, access access)
9492
| CustomOperator(symbol_table parent_symtab, identifier name,
@@ -358,6 +356,10 @@ ttype
358356
| Const(ttype type)
359357
| CPtr()
360358
| TypeParameter(identifier param, dimension* dims)
359+
| FunctionType(ttype* arg_types, ttype? return_var_type,
360+
abi abi, deftype deftype, string? bindc_name, bool elemental,
361+
bool pure, bool module, bool inline, bool static, ttype* type_params,
362+
symbol* restrictions, bool is_restriction)
361363

362364
restriction_arg = RestrictionArg(identifier restriction_name, symbol restriction_func)
363365

src/libasr/asr_scopes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ void SymbolTable::mark_all_variables_external(Allocator &/*al*/) {
8585
}
8686
case (ASR::symbolType::Function) : {
8787
ASR::Function_t *v = ASR::down_cast<ASR::Function_t>(a.second);
88-
v->m_abi = ASR::abiType::Interactive;
88+
ASR::FunctionType_t* v_func_type = ASR::down_cast<ASR::FunctionType_t>(v->m_function_signature);
89+
v_func_type->m_abi = ASR::abiType::Interactive;
8990
v->m_body = nullptr;
9091
v->n_body = 0;
9192
break;

src/libasr/asr_utils.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ void set_intrinsic(ASR::symbol_t* sym) {
265265
}
266266
case ASR::symbolType::Function: {
267267
ASR::Function_t* function_sym = ASR::down_cast<ASR::Function_t>(sym);
268-
function_sym->m_abi = ASR::abiType::Intrinsic;
268+
ASR::FunctionType_t* func_sym_type = ASR::down_cast<ASR::FunctionType_t>(function_sym->m_function_signature);
269+
func_sym_type->m_abi = ASR::abiType::Intrinsic;
269270
break;
270271
}
271272
case ASR::symbolType::StructType: {
@@ -431,7 +432,9 @@ bool use_overloaded(ASR::expr_t* left, ASR::expr_t* right,
431432
err("Unable to resolve matched function for operator overloading, " + matched_func_name, loc);
432433
}
433434
ASR::ttype_t *return_type = nullptr;
434-
if( func->m_elemental && func->n_args == 1 && ASRUtils::is_array(ASRUtils::expr_type(a_args[0].m_value)) ) {
435+
if( ASRUtils::get_FunctionType(func)->m_elemental &&
436+
func->n_args == 1 &&
437+
ASRUtils::is_array(ASRUtils::expr_type(a_args[0].m_value)) ) {
435438
return_type = ASRUtils::duplicate_type(al, ASRUtils::expr_type(a_args[0].m_value));
436439
} else {
437440
return_type = ASRUtils::expr_type(func->m_return_var);
@@ -813,7 +816,9 @@ bool use_overloaded(ASR::expr_t* left, ASR::expr_t* right,
813816
err("Unable to resolve matched function for operator overloading, " + matched_func_name, loc);
814817
}
815818
ASR::ttype_t *return_type = nullptr;
816-
if( func->m_elemental && func->n_args == 1 && ASRUtils::is_array(ASRUtils::expr_type(a_args[0].m_value)) ) {
819+
if( ASRUtils::get_FunctionType(func)->m_elemental &&
820+
func->n_args == 1 &&
821+
ASRUtils::is_array(ASRUtils::expr_type(a_args[0].m_value)) ) {
817822
return_type = ASRUtils::duplicate_type(al, ASRUtils::expr_type(a_args[0].m_value));
818823
} else {
819824
return_type = ASRUtils::expr_type(func->m_return_var);
@@ -968,7 +973,9 @@ ASR::asr_t* symbol_resolve_external_generic_procedure_without_eval(
968973
if( ASR::is_a<ASR::Function_t>(*final_sym) ) {
969974
ASR::Function_t* func = ASR::down_cast<ASR::Function_t>(final_sym);
970975
if (func->m_return_var) {
971-
if( func->m_elemental && func->n_args == 1 && ASRUtils::is_array(ASRUtils::expr_type(args[0].m_value)) ) {
976+
if( ASRUtils::get_FunctionType(func)->m_elemental &&
977+
func->n_args == 1 &&
978+
ASRUtils::is_array(ASRUtils::expr_type(args[0].m_value)) ) {
972979
return_type = ASRUtils::duplicate_type(al, ASRUtils::expr_type(args[0].m_value));
973980
} else {
974981
return_type = ASRUtils::EXPR2VAR(func->m_return_var)->m_type;

src/libasr/asr_utils.h

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,15 @@ static inline bool is_intrinsic_symbol(const ASR::symbol_t *fn) {
525525
return false;
526526
}
527527

528+
static inline ASR::FunctionType_t* get_FunctionType(const ASR::Function_t* x) {
529+
return ASR::down_cast<ASR::FunctionType_t>(x->m_function_signature);
530+
}
531+
532+
static inline ASR::FunctionType_t* get_FunctionType(const ASR::Function_t& x) {
533+
return ASR::down_cast<ASR::FunctionType_t>(x.m_function_signature);
534+
}
535+
536+
528537
// Returns true if the Function is intrinsic, otherwise false
529538
// This version uses the `intrinsic` member of `Module`, so it
530539
// should be used instead of is_intrinsic_procedure
@@ -533,7 +542,7 @@ static inline bool is_intrinsic_function2(const ASR::Function_t *fn) {
533542
ASR::Module_t *m = get_sym_module0(sym);
534543
if (m != nullptr) {
535544
if (m->m_intrinsic ||
536-
fn->m_abi == ASR::abiType::Intrinsic) {
545+
ASRUtils::get_FunctionType(fn)->m_abi == ASR::abiType::Intrinsic) {
537546
return true;
538547
}
539548
}
@@ -1336,7 +1345,8 @@ static inline bool is_generic_function(ASR::symbol_t *x) {
13361345
switch (x2->type) {
13371346
case ASR::symbolType::Function: {
13381347
ASR::Function_t *func_sym = ASR::down_cast<ASR::Function_t>(x2);
1339-
return func_sym->n_type_params > 0 && !func_sym->m_is_restriction;
1348+
return (ASRUtils::get_FunctionType(func_sym)->n_type_params > 0 &&
1349+
!ASRUtils::get_FunctionType(func_sym)->m_is_restriction);
13401350
}
13411351
default: return false;
13421352
}
@@ -1347,7 +1357,7 @@ static inline bool is_restriction_function(ASR::symbol_t *x) {
13471357
switch (x2->type) {
13481358
case ASR::symbolType::Function: {
13491359
ASR::Function_t *func_sym = ASR::down_cast<ASR::Function_t>(x2);
1350-
return func_sym->m_is_restriction;
1360+
return ASRUtils::get_FunctionType(func_sym)->m_is_restriction;
13511361
}
13521362
default: return false;
13531363
}
@@ -2285,6 +2295,34 @@ static inline bool is_pass_array_by_data_possible(ASR::Function_t* x, std::vecto
22852295
return v.size() > 0;
22862296
}
22872297

2298+
inline ASR::asr_t* make_Function_t_util(Allocator& al, const Location& loc,
2299+
SymbolTable* m_symtab, char* m_name, char** m_dependencies, size_t n_dependencies,
2300+
ASR::expr_t** a_args, size_t n_args, ASR::stmt_t** m_body, size_t n_body,
2301+
ASR::expr_t* m_return_var, ASR::abiType m_abi, ASR::accessType m_access,
2302+
ASR::deftypeType m_deftype, char* m_bindc_name, bool m_elemental, bool m_pure,
2303+
bool m_module, bool m_inline, bool m_static, ASR::ttype_t** m_type_params,
2304+
size_t n_type_params, ASR::symbol_t** m_restrictions, size_t n_restrictions,
2305+
bool m_is_restriction, bool m_deterministic, bool m_side_effect_free) {
2306+
Vec<ASR::ttype_t*> arg_types;
2307+
arg_types.reserve(al, n_args);
2308+
for( size_t i = 0; i < n_args; i++ ) {
2309+
arg_types.push_back(al, ASRUtils::expr_type(a_args[i]));
2310+
}
2311+
ASR::ttype_t* return_var_type = nullptr;
2312+
if( m_return_var ) {
2313+
return_var_type = ASRUtils::expr_type(m_return_var);
2314+
}
2315+
ASR::ttype_t* func_type = ASRUtils::TYPE(ASR::make_FunctionType_t(
2316+
al, loc, arg_types.p, arg_types.size(), return_var_type, m_abi,
2317+
m_deftype, m_bindc_name, m_elemental, m_pure, m_module, m_inline,
2318+
m_static, m_type_params, n_type_params, m_restrictions, n_restrictions,
2319+
m_is_restriction));
2320+
return ASR::make_Function_t(
2321+
al, loc, m_symtab, m_name, func_type, m_dependencies, n_dependencies,
2322+
a_args, n_args, m_body, n_body, m_return_var, m_access, m_deterministic,
2323+
m_side_effect_free);
2324+
}
2325+
22882326
static inline ASR::expr_t* get_bound(ASR::expr_t* arr_expr, int dim,
22892327
std::string bound, Allocator& al) {
22902328
ASR::ttype_t* int32_type = ASRUtils::TYPE(ASR::make_Integer_t(al, arr_expr->base.loc,

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,10 @@ R"(#include <stdio.h>
327327
template_for_Kokkos.clear();
328328
template_number = 0;
329329
std::string sub, inl, static_attr;
330-
if (x.m_inline) {
330+
if (ASRUtils::get_FunctionType(x)->m_inline) {
331331
inl = "inline __attribute__((always_inline)) ";
332332
}
333-
if( x.m_static ) {
333+
if( ASRUtils::get_FunctionType(x)->m_static ) {
334334
static_attr = "static ";
335335
}
336336
if (x.m_return_var) {
@@ -471,8 +471,8 @@ R"(#include <stdio.h>
471471
sym_info[get_hash((ASR::asr_t*)&x)] = s;
472472
}
473473
std::string sub = get_function_declaration(x);
474-
if (x.m_abi == ASR::abiType::BindC
475-
&& x.m_deftype == ASR::deftypeType::Interface) {
474+
if (ASRUtils::get_FunctionType(x)->m_abi == ASR::abiType::BindC
475+
&& ASRUtils::get_FunctionType(x)->m_deftype == ASR::deftypeType::Interface) {
476476
sub += ";\n";
477477
} else {
478478
sub += "\n";

src/libasr/codegen/asr_to_julia.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ class ASRToJuliaVisitor : public ASR::BaseVisitor<ASRToJuliaVisitor>
425425
std::string get_function_declaration(const ASR::Function_t& x)
426426
{
427427
std::string sub, inl, ret_type;
428-
if (x.m_inline) {
428+
if (ASRUtils::get_FunctionType(x)->m_inline) {
429429
inl = "@inline ";
430430
}
431431
if (x.m_return_var) {
@@ -665,7 +665,8 @@ class ASRToJuliaVisitor : public ASR::BaseVisitor<ASRToJuliaVisitor>
665665
sym_info[get_hash((ASR::asr_t*) &x)] = s;
666666
}
667667
std::string sub = get_function_declaration(x);
668-
if (x.m_abi == ASR::abiType::BindC && x.m_deftype == ASR::deftypeType::Interface) {
668+
if (ASRUtils::get_FunctionType(x)->m_abi == ASR::abiType::BindC &&
669+
ASRUtils::get_FunctionType(x)->m_deftype == ASR::deftypeType::Interface) {
669670
} else {
670671
indentation_level += 1;
671672
std::string indent(indentation_level * indentation_spaces, ' ');

0 commit comments

Comments
 (0)