Skip to content

Commit

Permalink
Merge pull request #42 from certik/czgdp1807_lc_10
Browse files Browse the repository at this point in the history
Handle iterators with same name in different loops
  • Loading branch information
czgdp1807 authored Dec 12, 2023
2 parents 7e3fc1a + 85cdfaf commit 82a254b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
39 changes: 38 additions & 1 deletion src/lc/clang_ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
ASR::asr_t* tmp;
Vec<ASR::stmt_t*>* current_body;
bool is_stmt_created;
std::vector<std::map<std::string, std::string>> scopes;

explicit ClangASTtoASRVisitor(clang::ASTContext *Context_,
Allocator& al_, ASR::asr_t*& tu_):
Expand Down Expand Up @@ -399,6 +400,19 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit

bool TraverseVarDecl(clang::VarDecl *x) {
std::string name = x->getName().str();
if( scopes.size() > 0 ) {
if( scopes.back().find(name) != scopes.back().end() ) {
throw std::runtime_error(name + std::string(" is already defined."));
} else {
std::string aliased_name = current_scope->get_unique_name(name);
scopes.back()[name] = aliased_name;
name = aliased_name;
}
} else {
if( current_scope->resolve_symbol(name) ) {
throw std::runtime_error(name + std::string(" is already defined."));
}
}
ASR::ttype_t *asr_type = ClangTypeToASRType(x->getType());
ASR::symbol_t *v = ASR::down_cast<ASR::symbol_t>(ASR::make_Variable_t(al, Lloc(x),
current_scope, s2c(al, name), nullptr, 0, ASR::intentType::Local, nullptr, nullptr,
Expand Down Expand Up @@ -560,9 +574,29 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
return true;
}

ASR::symbol_t* check_aliases(std::string name) {
for( auto itr = scopes.rbegin(); itr != scopes.rend(); itr++ ) {
std::map<std::string, std::string>& alias = *itr;
if( alias.find(name) != alias.end() ) {
return current_scope->resolve_symbol(alias[name]);
}
}

return nullptr;
}

ASR::symbol_t* resolve_symbol(std::string name) {
ASR::symbol_t* sym = check_aliases(name);
if( sym ) {
return sym;
}

return current_scope->resolve_symbol(name);
}

bool TraverseDeclRefExpr(clang::DeclRefExpr* x) {
std::string name = x->getNameInfo().getAsString();
ASR::symbol_t* sym = current_scope->resolve_symbol(name);
ASR::symbol_t* sym = resolve_symbol(name);
LCOMPILERS_ASSERT(sym != nullptr);
tmp = ASR::make_Var_t(al, Lloc(x), sym);
is_stmt_created = false;
Expand Down Expand Up @@ -692,6 +726,8 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
}

bool TraverseForStmt(clang::ForStmt* x) {
std::map<std::string, std::string> alias;
scopes.push_back(alias);
clang::Stmt* init_stmt = x->getInit();
TraverseStmt(init_stmt);
LCOMPILERS_ASSERT(tmp != nullptr && is_stmt_created);
Expand All @@ -714,6 +750,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit

tmp = ASR::make_WhileLoop_t(al, Lloc(x), nullptr, test, body.p, body.size());
is_stmt_created = true;
scopes.pop_back();
return true;
}

Expand Down
11 changes: 5 additions & 6 deletions tests/array_02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ float matmul_test(int m, int n, int nums) {
float arr1d[n];
float arr2d[nums][m][n];
float sum;
float output = 0.0;
for( int num = 0; num < nums; num++ ) {
for( int i = 0; i < m; i++ ) {
for( int j = 0; j < n; j++ ) {
Expand All @@ -22,11 +21,11 @@ float matmul_test(int m, int n, int nums) {
}

sum = 0.0;
for( int num1 = 0; num1 < nums; num1++ ) {
for( int i1 = 0; i1 < M; i1++ ) {
output = 0.0;
for( int j1 = 0; j1 < N; j1++ ) {
output += arr2d[num1][i1][j1] * arr1d[j1];
for( int num = 0; num < nums; num++ ) {
for( int i = 0; i < M; i++ ) {
float output = 0.0;
for( int j = 0; j < N; j++ ) {
output += arr2d[num][i][j] * arr1d[j];
}
sum += output;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/asr-array_02-0507073.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-array_02-0507073",
"cmd": "lc --asr-dump --no-color {infile} -o {outfile} -extra-arg=\"-Isrc/runtime/include\"",
"infile": "tests/array_02.cpp",
"infile_hash": "08d915c81156be4dd97850551458df249dbd05e24685d14350a33c5b",
"infile_hash": "cb968d294be0d86c0f49b3364b7a3aa45b53edcc59349301d5f298ed",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_02-0507073.stdout",
"stdout_hash": "f560c32a87c22412ad843882ffed66654d10e186ead0dd9b32f400de",
"stdout_hash": "0baf3d16bb054881ea07f3b263006e49d1cbbad16dafe435cbd78e35",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
13 changes: 0 additions & 13 deletions tests/reference/asr-array_02-0507073.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -412,19 +412,6 @@
(Var 3 n)
(Var 3 nums)]
[(=
(Var 3 output)
(Cast
(RealConstant
0.000000
(Real 8)
)
RealToReal
(Real 4)
()
)
()
)
(=
(Var 3 num)
(IntegerConstant 0 (Integer 4))
()
Expand Down

0 comments on commit 82a254b

Please sign in to comment.