Skip to content

Commit 5197896

Browse files
committed
Sync other needed files
1 parent 4c496d5 commit 5197896

File tree

6 files changed

+67
-14
lines changed

6 files changed

+67
-14
lines changed

src/libasr/asdl_cpp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,12 +1775,12 @@ def visitField(self, field, cons):
17751775
elif field.type == "string" and not field.seq:
17761776
if field.opt:
17771777
self.emit("if (x.m_%s) {" % field.name, 2)
1778-
self.emit( 's.append("\\"" + std::string(x.m_%s) + "\\"");' % field.name, 3)
1778+
self.emit( 's.append("\\"" + get_escaped_str(x.m_%s) + "\\"");' % field.name, 3)
17791779
self.emit("} else {", 2)
17801780
self.emit( 's.append("[]");', 3)
17811781
self.emit("}", 2)
17821782
else:
1783-
self.emit('s.append("\\"" + std::string(x.m_%s) + "\\"");' % field.name, 2)
1783+
self.emit('s.append("\\"" + get_escaped_str(x.m_%s) + "\\"");' % field.name, 2)
17841784
elif field.type == "int" and not field.seq:
17851785
if field.opt:
17861786
self.emit("if (x.m_%s) {" % field.name, 2)
@@ -2495,6 +2495,7 @@ def add_masks(fields, node):
24952495
#include <libasr/containers.h>
24962496
#include <libasr/exception.h>
24972497
#include <libasr/asr_scopes.h>
2498+
#include <libasr/string_utils.h>
24982499
24992500
25002501
namespace LCompilers::%(MOD)s {

src/libasr/pass/pass_array_by_data.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class PassArrayByDataProcedureVisitor : public PassUtils::PassVisitor<PassArrayB
201201
return_var, x_func_type->m_abi, x->m_access, x_func_type->m_deftype,
202202
s2c(al, new_bindc_name), x_func_type->m_elemental,
203203
x_func_type->m_pure, x_func_type->m_module, x_func_type->m_inline,
204-
x_func_type->m_static, nullptr, 0, nullptr, 0, false, false, false);
204+
x_func_type->m_static, x_func_type->m_type_params, x_func_type->n_type_params, nullptr, 0, false, false, false);
205205
new_symbol = ASR::down_cast<ASR::symbol_t>(new_subrout);
206206
}
207207
current_scope->add_symbol(new_name, new_symbol);

src/libasr/pass/unused_functions.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ class CollectUnusedFunctionsVisitor :
2323
std::map<uint64_t, std::string> fn_declarations;
2424
std::map<uint64_t, std::string> fn_used;
2525

26-
// TODO: Do subroutines just like Functions:
27-
2826
void visit_Function(const ASR::Function_t &x) {
29-
if (x.m_return_var) {
30-
uint64_t h = get_hash((ASR::asr_t*)&x);
31-
if (ASRUtils::get_FunctionType(x)->m_abi != ASR::abiType::BindC) {
32-
fn_declarations[h] = x.m_name;
33-
}
27+
uint64_t h = get_hash((ASR::asr_t*)&x);
28+
if (ASRUtils::get_FunctionType(x)->m_abi != ASR::abiType::BindC) {
29+
fn_declarations[h] = x.m_name;
3430
}
3531

3632
for( size_t i = 0; i < x.n_args; i++ ) {
@@ -56,8 +52,7 @@ class CollectUnusedFunctionsVisitor :
5652
}
5753

5854
void visit_ExternalSymbol(const ASR::ExternalSymbol_t &x) {
59-
if (ASR::is_a<ASR::Function_t>(*x.m_external) &&
60-
ASR::down_cast<ASR::Function_t>(x.m_external)->m_return_var) {
55+
if (ASR::is_a<ASR::Function_t>(*x.m_external)) {
6156
uint64_t h = get_hash((ASR::asr_t*)&x);
6257
fn_declarations[h] = x.m_name;
6358
h = get_hash((ASR::asr_t*)x.m_external);
@@ -72,7 +67,8 @@ class CollectUnusedFunctionsVisitor :
7267
}
7368

7469

75-
void visit_FunctionCall(const ASR::FunctionCall_t &x) {
70+
template <typename T>
71+
void visit_FuncSubCall(const T& x) {
7672
{
7773
const ASR::symbol_t *s = ASRUtils::symbol_get_past_external(x.m_name);
7874
if (ASR::is_a<ASR::Function_t>(*s)) {
@@ -115,14 +111,32 @@ class CollectUnusedFunctionsVisitor :
115111
if( x.m_args[i].m_value ) {
116112
visit_expr(*(x.m_args[i].m_value));
117113
}
114+
if (x.m_args[i].m_value && ASR::is_a<ASR::Var_t>(*x.m_args[i].m_value)) {
115+
ASR::Var_t* var = ASR::down_cast<ASR::Var_t>(x.m_args[i].m_value);
116+
if (ASR::is_a<ASR::ExternalSymbol_t>(*var->m_v)) {
117+
ASR::ExternalSymbol_t* extsym = ASR::down_cast<ASR::ExternalSymbol_t>(var->m_v);
118+
uint64_t h = get_hash((ASR::asr_t*)extsym);
119+
fn_used[h] = extsym->m_name;
120+
}
121+
}
118122
}
123+
}
124+
125+
void visit_FunctionCall(const ASR::FunctionCall_t &x) {
126+
visit_FuncSubCall(x);
119127
visit_ttype(*x.m_type);
120128
if (x.m_value)
121129
visit_expr(*x.m_value);
122130
if (x.m_dt)
123131
visit_expr(*x.m_dt);
124132
}
125133

134+
void visit_SubroutineCall(const ASR::SubroutineCall_t &x) {
135+
visit_FuncSubCall(x);
136+
if (x.m_dt)
137+
visit_expr(*x.m_dt);
138+
}
139+
126140
void visit_Var(const ASR::Var_t &x) {
127141
const ASR::symbol_t *s = ASRUtils::symbol_get_past_external(x.m_v);
128142
if (ASR::is_a<ASR::Function_t>(*s)) {
@@ -182,6 +196,15 @@ class ProgramVisitor :
182196
public:
183197
bool program_present=false;
184198

199+
void visit_TranslationUnit(const ASR::TranslationUnit_t &x) {
200+
for (auto &a : x.m_global_scope->get_scope()) {
201+
if (ASR::is_a<ASR::Program_t>(*a.second)) {
202+
this->visit_symbol(*a.second);
203+
break;
204+
}
205+
}
206+
}
207+
185208
void visit_Program(const ASR::Program_t &/*x*/) {
186209
program_present = true;
187210
}

src/libasr/pass/update_array_dim_intrinsic_calls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ReplaceArrayDimIntrinsicCalls: public ASR::BaseExprReplacer<ReplaceArrayDi
6666
ASR::ttype_t* array_type = ASRUtils::expr_type(x->m_v);
6767
ASR::dimension_t* dims = nullptr;
6868
int n = ASRUtils::extract_dimensions_from_ttype(array_type, dims);
69-
bool is_argument = v->m_intent == ASRUtils::intent_in || v->m_intent == ASRUtils::intent_out;
69+
bool is_argument = v->m_intent == ASRUtils::intent_in || v->m_intent == ASRUtils::intent_out || v->m_intent == ASRUtils::intent_inout;
7070
if( !(n > 0 && is_argument &&
7171
!ASRUtils::is_dimension_empty(dims, n)) ) {
7272
return ;

src/libasr/string_utils.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <string>
55
#include <iostream>
66
#include <fstream>
7+
#include <iomanip>
8+
#include <sstream>
79

810
#include <libasr/string_utils.h>
911
#include <libasr/containers.h>
@@ -85,6 +87,29 @@ std::string replace(const std::string &s,
8587
return std::regex_replace(s, std::regex(regex), replace);
8688
}
8789

90+
std::string get_escaped_str(const std::string &s) {
91+
std::ostringstream o;
92+
for (auto c = s.cbegin(); c != s.cend(); c++) {
93+
switch (*c) {
94+
case '"': o << "\\\""; break;
95+
case '\\': o << "\\\\"; break;
96+
case '\b': o << "\\b"; break;
97+
case '\f': o << "\\f"; break;
98+
case '\n': o << "\\n"; break;
99+
case '\r': o << "\\r"; break;
100+
case '\t': o << "\\t"; break;
101+
default:
102+
if ('\x00' <= *c && *c <= '\x1f') {
103+
o << "\\u"
104+
<< std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(*c);
105+
} else {
106+
o << *c;
107+
}
108+
}
109+
}
110+
return o.str();
111+
}
112+
88113
std::string read_file(const std::string &filename)
89114
{
90115
std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary

src/libasr/string_utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ char *s2c(Allocator &al, const std::string &s);
2424
std::string replace(const std::string &s,
2525
const std::string &regex, const std::string &replace);
2626

27+
// Escapes special characters from the given string.
28+
// It is used during AST/R to Json conversion.
29+
std::string get_escaped_str(const std::string &s);
30+
2731
std::string read_file(const std::string &filename);
2832

2933
// Returns the parent path to the given path

0 commit comments

Comments
 (0)