Skip to content

Commit c84ad07

Browse files
committed
Sync libasr with LFortran
1 parent e9e2144 commit c84ad07

23 files changed

+1227
-400
lines changed

src/libasr/ASR.asdl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ symbol
107107
intent intent, expr? symbolic_value, expr? value, storage_type storage, ttype type,
108108
abi abi, access access, presence presence, bool value_attr)
109109
| ClassType(symbol_table symtab, identifier name, abi abi, access access)
110-
| ClassProcedure(symbol_table parent_symtab, identifier name, identifier proc_name,
111-
symbol proc, abi abi)
110+
| ClassProcedure(symbol_table parent_symtab, identifier name, identifier? self_argument,
111+
identifier proc_name, symbol proc, abi abi)
112112
| AssociateBlock(symbol_table symtab, identifier name, stmt* body)
113113
| Block(symbol_table symtab, identifier name, stmt* body)
114114

@@ -185,6 +185,7 @@ stmt
185185
| FileOpen(int label, expr? newunit, expr? filename, expr? status)
186186
| FileClose(int label, expr? unit, expr? iostat, expr? iomsg, expr? err, expr? status)
187187
| FileRead(int label, expr? unit, expr? fmt, expr? iomsg, expr? iostat, expr? id, expr* values)
188+
| FileBackspace(int label, expr? unit, expr? iostat, expr? err)
188189
| FileRewind(int label, expr? unit, expr? iostat, expr? err)
189190
| FileInquire(int label, expr? unit, expr? file, expr? iostat, expr? err,
190191
expr? exist, expr? opened, expr? number, expr? named,
@@ -206,6 +207,7 @@ stmt
206207
| Flush(int label, expr unit, expr? err, expr? iomsg, expr? iostat)
207208
| ListAppend(expr a, expr ele)
208209
| AssociateBlockCall(symbol m)
210+
| SelectType(type_stmt* body, stmt* default)
209211
| CPtrToPointer(expr cptr, expr ptr, expr? shape)
210212
| BlockCall(int label, symbol m)
211213
| SetInsert(expr a, expr ele)
@@ -285,6 +287,7 @@ expr
285287
| ArrayMatMul(expr matrix_a, expr matrix_b, ttype type, expr? value)
286288
| ArrayPack(expr array, expr mask, expr? vector, ttype type, expr? value)
287289
| ArrayReshape(expr array, expr shape, ttype type, expr? value)
290+
| ArrayMaxloc(expr array, expr? dim, expr? mask, expr? kind, expr? back, ttype type, expr? value)
288291

289292
| BitCast(expr source, expr mold, expr? size, ttype type, expr? value)
290293
| StructInstanceMember(expr v, symbol m, ttype type, expr? value)
@@ -310,9 +313,13 @@ expr
310313
| SetPop(expr a, ttype type, expr? value)
311314
| IntegerBitLen(expr a, ttype type, expr? value)
312315
| Ichar(expr arg, ttype type, expr? value)
316+
| Iachar(expr arg, ttype type, expr? value)
313317

314318
| SizeOfType(ttype arg, ttype type, expr? value)
315319

320+
| PointerNullConstant(ttype type)
321+
| PointerAssociated(expr ptr, expr? tgt, ttype type, expr? value)
322+
316323

317324
-- `len` in Character:
318325
-- >=0 ... the length of the string, known at compile time
@@ -406,6 +413,8 @@ do_loop_head = (expr? v, expr? start, expr? end, expr? increment)
406413

407414
case_stmt = CaseStmt(expr* test, stmt* body) | CaseStmt_Range(expr? start, expr? end, stmt* body)
408415

416+
type_stmt = TypeStmt(symbol sym, stmt* body)
417+
409418
enumtype = IntegerConsecutiveFromZero | IntegerUnique | IntegerNotUnique | NonInteger
410419

411420
}

src/libasr/asdl_cpp.py

Lines changed: 313 additions & 39 deletions
Large diffs are not rendered by default.

src/libasr/asr_utils.cpp

Lines changed: 240 additions & 161 deletions
Large diffs are not rendered by default.

src/libasr/asr_utils.h

Lines changed: 93 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ static inline ASR::ttype_t* symbol_type(const ASR::symbol_t *f)
103103
case ASR::symbolType::EnumType: {
104104
return ASR::down_cast<ASR::EnumType_t>(f)->m_type;
105105
}
106+
case ASR::symbolType::ExternalSymbol: {
107+
return symbol_type(ASRUtils::symbol_get_past_external(f));
108+
}
109+
case ASR::symbolType::Function: {
110+
return ASRUtils::expr_type(
111+
ASR::down_cast<ASR::Function_t>(f)->m_return_var);
112+
}
106113
default: {
107114
throw LCompilersException("Cannot return type of, " +
108115
std::to_string(f->type) + " symbol.");
@@ -175,6 +182,52 @@ static inline ASR::abiType expr_abi(ASR::expr_t* e) {
175182
}
176183
}
177184

185+
static inline char *symbol_name(const ASR::symbol_t *f)
186+
{
187+
switch (f->type) {
188+
case ASR::symbolType::Program: {
189+
return ASR::down_cast<ASR::Program_t>(f)->m_name;
190+
}
191+
case ASR::symbolType::Module: {
192+
return ASR::down_cast<ASR::Module_t>(f)->m_name;
193+
}
194+
case ASR::symbolType::Function: {
195+
return ASR::down_cast<ASR::Function_t>(f)->m_name;
196+
}
197+
case ASR::symbolType::GenericProcedure: {
198+
return ASR::down_cast<ASR::GenericProcedure_t>(f)->m_name;
199+
}
200+
case ASR::symbolType::StructType: {
201+
return ASR::down_cast<ASR::StructType_t>(f)->m_name;
202+
}
203+
case ASR::symbolType::EnumType: {
204+
return ASR::down_cast<ASR::EnumType_t>(f)->m_name;
205+
}
206+
case ASR::symbolType::UnionType: {
207+
return ASR::down_cast<ASR::UnionType_t>(f)->m_name;
208+
}
209+
case ASR::symbolType::Variable: {
210+
return ASR::down_cast<ASR::Variable_t>(f)->m_name;
211+
}
212+
case ASR::symbolType::ExternalSymbol: {
213+
return ASR::down_cast<ASR::ExternalSymbol_t>(f)->m_name;
214+
}
215+
case ASR::symbolType::ClassProcedure: {
216+
return ASR::down_cast<ASR::ClassProcedure_t>(f)->m_name;
217+
}
218+
case ASR::symbolType::CustomOperator: {
219+
return ASR::down_cast<ASR::CustomOperator_t>(f)->m_name;
220+
}
221+
case ASR::symbolType::AssociateBlock: {
222+
return ASR::down_cast<ASR::AssociateBlock_t>(f)->m_name;
223+
}
224+
case ASR::symbolType::Block: {
225+
return ASR::down_cast<ASR::Block_t>(f)->m_name;
226+
}
227+
default : throw LCompilersException("Not implemented");
228+
}
229+
}
230+
178231
static inline std::string type_to_str(const ASR::ttype_t *t)
179232
{
180233
switch (t->type) {
@@ -206,7 +259,7 @@ static inline std::string type_to_str(const ASR::ttype_t *t)
206259
return "list";
207260
}
208261
case ASR::ttypeType::Struct: {
209-
return "derived type";
262+
return ASRUtils::symbol_name(ASR::down_cast<ASR::Struct_t>(t)->m_derived_type);
210263
}
211264
case ASR::ttypeType::Union: {
212265
return "union";
@@ -267,52 +320,6 @@ static inline ASR::expr_t* expr_value(ASR::expr_t *f)
267320
return ASR::expr_value0(f);
268321
}
269322

270-
static inline char *symbol_name(const ASR::symbol_t *f)
271-
{
272-
switch (f->type) {
273-
case ASR::symbolType::Program: {
274-
return ASR::down_cast<ASR::Program_t>(f)->m_name;
275-
}
276-
case ASR::symbolType::Module: {
277-
return ASR::down_cast<ASR::Module_t>(f)->m_name;
278-
}
279-
case ASR::symbolType::Function: {
280-
return ASR::down_cast<ASR::Function_t>(f)->m_name;
281-
}
282-
case ASR::symbolType::GenericProcedure: {
283-
return ASR::down_cast<ASR::GenericProcedure_t>(f)->m_name;
284-
}
285-
case ASR::symbolType::StructType: {
286-
return ASR::down_cast<ASR::StructType_t>(f)->m_name;
287-
}
288-
case ASR::symbolType::EnumType: {
289-
return ASR::down_cast<ASR::EnumType_t>(f)->m_name;
290-
}
291-
case ASR::symbolType::UnionType: {
292-
return ASR::down_cast<ASR::UnionType_t>(f)->m_name;
293-
}
294-
case ASR::symbolType::Variable: {
295-
return ASR::down_cast<ASR::Variable_t>(f)->m_name;
296-
}
297-
case ASR::symbolType::ExternalSymbol: {
298-
return ASR::down_cast<ASR::ExternalSymbol_t>(f)->m_name;
299-
}
300-
case ASR::symbolType::ClassProcedure: {
301-
return ASR::down_cast<ASR::ClassProcedure_t>(f)->m_name;
302-
}
303-
case ASR::symbolType::CustomOperator: {
304-
return ASR::down_cast<ASR::CustomOperator_t>(f)->m_name;
305-
}
306-
case ASR::symbolType::AssociateBlock: {
307-
return ASR::down_cast<ASR::AssociateBlock_t>(f)->m_name;
308-
}
309-
case ASR::symbolType::Block: {
310-
return ASR::down_cast<ASR::Block_t>(f)->m_name;
311-
}
312-
default : throw LCompilersException("Not implemented");
313-
}
314-
}
315-
316323
static inline std::pair<char**, size_t> symbol_dependencies(const ASR::symbol_t *f)
317324
{
318325
switch (f->type) {
@@ -1231,6 +1238,8 @@ bool use_overloaded(ASR::expr_t* left, ASR::expr_t* right,
12311238
ASR::binopType op, std::string& intrinsic_op_name,
12321239
SymbolTable* curr_scope, ASR::asr_t*& asr,
12331240
Allocator &al, const Location& loc,
1241+
std::set<std::string>& current_function_dependencies,
1242+
Vec<char*>& current_module_dependencies,
12341243
const std::function<void (const std::string &, const Location &)> err);
12351244

12361245
bool is_op_overloaded(ASR::binopType op, std::string& intrinsic_op_name,
@@ -1240,6 +1249,8 @@ bool use_overloaded(ASR::expr_t* left, ASR::expr_t* right,
12401249
ASR::cmpopType op, std::string& intrinsic_op_name,
12411250
SymbolTable* curr_scope, ASR::asr_t*& asr,
12421251
Allocator &al, const Location& loc,
1252+
std::set<std::string>& current_function_dependencies,
1253+
Vec<char*>& current_module_dependencies,
12431254
const std::function<void (const std::string &, const Location &)> err);
12441255

12451256
bool is_op_overloaded(ASR::cmpopType op, std::string& intrinsic_op_name,
@@ -1248,6 +1259,8 @@ bool is_op_overloaded(ASR::cmpopType op, std::string& intrinsic_op_name,
12481259
bool use_overloaded_assignment(ASR::expr_t* target, ASR::expr_t* value,
12491260
SymbolTable* curr_scope, ASR::asr_t*& asr,
12501261
Allocator &al, const Location& loc,
1262+
std::set<std::string>& current_function_dependencies,
1263+
Vec<char*>& /*current_module_dependencies*/,
12511264
const std::function<void (const std::string &, const Location &)> err);
12521265

12531266
void set_intrinsic(ASR::symbol_t* sym);
@@ -1318,6 +1331,27 @@ static inline bool is_generic(ASR::ttype_t &x) {
13181331
}
13191332
}
13201333

1334+
static inline bool is_generic_function(ASR::symbol_t *x) {
1335+
ASR::symbol_t* x2 = symbol_get_past_external(x);
1336+
switch (x2->type) {
1337+
case ASR::symbolType::Function: {
1338+
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;
1340+
}
1341+
default: return false;
1342+
}
1343+
}
1344+
1345+
static inline bool is_restriction_function(ASR::symbol_t *x) {
1346+
ASR::symbol_t* x2 = symbol_get_past_external(x);
1347+
switch (x2->type) {
1348+
case ASR::symbolType::Function: {
1349+
ASR::Function_t *func_sym = ASR::down_cast<ASR::Function_t>(x2);
1350+
return func_sym->m_is_restriction;
1351+
}
1352+
default: return false;
1353+
}
1354+
}
13211355

13221356
static inline int get_body_size(ASR::symbol_t* s) {
13231357
int n_body = 0;
@@ -1678,7 +1712,12 @@ inline bool is_same_type_pointer(ASR::ttype_t* source, ASR::ttype_t* dest) {
16781712
source = dest;
16791713
dest = temp;
16801714
}
1681-
bool res = source->type == ASR::down_cast<ASR::Pointer_t>(dest)->m_type->type;
1715+
dest = ASR::down_cast<ASR::Pointer_t>(dest)->m_type;
1716+
if( (ASR::is_a<ASR::Class_t>(*source) || ASR::is_a<ASR::Struct_t>(*source)) &&
1717+
(ASR::is_a<ASR::Class_t>(*dest) || ASR::is_a<ASR::Struct_t>(*dest)) ) {
1718+
return true;
1719+
}
1720+
bool res = source->type == dest->type;
16821721
return res;
16831722
}
16841723

@@ -1923,12 +1962,15 @@ class ReplaceArgVisitor: public ASR::BaseExprReplacer<ReplaceArgVisitor> {
19231962

19241963
Vec<ASR::call_arg_t>& orig_args;
19251964

1965+
std::set<std::string>& current_function_dependencies;
1966+
19261967
public:
19271968

19281969
ReplaceArgVisitor(Allocator& al_, SymbolTable* current_scope_,
1929-
ASR::Function_t* orig_func_, Vec<ASR::call_arg_t>& orig_args_) :
1970+
ASR::Function_t* orig_func_, Vec<ASR::call_arg_t>& orig_args_,
1971+
std::set<std::string>& current_function_dependencies_) :
19301972
al(al_), current_scope(current_scope_), orig_func(orig_func_),
1931-
orig_args(orig_args_)
1973+
orig_args(orig_args_), current_function_dependencies(current_function_dependencies_)
19321974
{}
19331975

19341976
void replace_FunctionCall(ASR::FunctionCall_t* x) {
@@ -1984,6 +2026,7 @@ class ReplaceArgVisitor: public ASR::BaseExprReplacer<ReplaceArgVisitor> {
19842026
replace_expr(x->m_args[i].m_value);
19852027
current_expr = current_expr_copy_;
19862028
}
2029+
current_function_dependencies.insert(std::string(ASRUtils::symbol_name(new_es)));
19872030
x->m_name = new_es;
19882031
}
19892032

0 commit comments

Comments
 (0)