Skip to content

Commit d7f6115

Browse files
committed
C: Fix nullptr current scope issue
1 parent ea2d3d7 commit d7f6115

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/libasr/codegen/asr_to_c.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
583583
std::string unit_src = "";
584584
indentation_level = 0;
585585
indentation_spaces = 4;
586+
SymbolTable* current_scope_copy = current_scope;
587+
current_scope = global_scope;
586588
c_ds_api->set_indentation(indentation_level, indentation_spaces);
587589
c_ds_api->set_global_scope(global_scope);
588590
c_utils_functions->set_indentation(indentation_level, indentation_spaces);
@@ -760,6 +762,7 @@ R"(
760762
out_file.close();
761763
}
762764
}
765+
current_scope = current_scope_copy;
763766
}
764767

765768
void visit_Module(const ASR::Module_t &x) {
@@ -768,7 +771,8 @@ R"(
768771
} else {
769772
intrinsic_module = false;
770773
}
771-
774+
SymbolTable *current_scope_copy = current_scope;
775+
current_scope = x.m_symtab;
772776
std::string unit_src = "";
773777
for (auto &item : x.m_symtab->get_scope()) {
774778
if (ASR::is_a<ASR::Variable_t>(*item.second)) {
@@ -813,13 +817,15 @@ R"(
813817
}
814818
src = unit_src;
815819
intrinsic_module = false;
820+
current_scope = current_scope_copy;
816821
}
817822

818823
void visit_Program(const ASR::Program_t &x) {
819824
// Topologically sort all program functions
820825
// and then define them in the right order
821826
std::vector<std::string> func_order = ASRUtils::determine_function_definition_order(x.m_symtab);
822-
827+
SymbolTable *current_scope_copy = current_scope;
828+
current_scope = x.m_symtab;
823829
// Generate code for nested subroutines and functions first:
824830
std::string contains;
825831
for (auto &item : func_order) {
@@ -898,6 +904,7 @@ R"( // Initialise Numpy
898904
+ decl + body
899905
+ indent1 + "return 0;\n}\n";
900906
indentation_level -= 2;
907+
current_scope = current_scope_copy;
901908
}
902909

903910
template <typename T>

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
193193

194194
void visit_TranslationUnit(const ASR::TranslationUnit_t &x) {
195195
global_scope = x.m_global_scope;
196+
SymbolTable current_scope_copy = current_scope;
197+
current_scope = global_scope;
196198
// All loose statements must be converted to a function, so the items
197199
// must be empty:
198200
LCOMPILERS_ASSERT(x.n_items == 0);
@@ -255,6 +257,7 @@ R"(#include <stdio.h>
255257
}
256258

257259
src = unit_src;
260+
current_scope = current_scope_copy;
258261
}
259262

260263
std::string check_tmp_buffer() {

0 commit comments

Comments
 (0)