Skip to content

Commit 89e74c5

Browse files
authored
Merge pull request #1273 from Shaikh-Ubaid/pass_compiler_options_as_arg
Pass CompilerOptions as argument to python_ast_to_asr()
2 parents 5084897 + ed87f79 commit 89e74c5

File tree

3 files changed

+28
-55
lines changed

3 files changed

+28
-55
lines changed

src/bin/lpython.cpp

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@ int emit_asr(const std::string &infile,
164164

165165
diagnostics.diagnostics.clear();
166166
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
167-
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
168-
compiler_options.disable_main, compiler_options.symtab_only, infile,
169-
compiler_options.import_path);
167+
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
170168
std::cerr << diagnostics.render(input, lm, compiler_options);
171169
if (!r.ok) {
172170
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -208,9 +206,7 @@ int emit_cpp(const std::string &infile,
208206

209207
diagnostics.diagnostics.clear();
210208
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
211-
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
212-
compiler_options.disable_main, compiler_options.symtab_only, infile,
213-
compiler_options.import_path);
209+
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
214210
std::cerr << diagnostics.render(input, lm, compiler_options);
215211
if (!r1.ok) {
216212
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -250,9 +246,7 @@ int emit_c(const std::string &infile,
250246

251247
diagnostics.diagnostics.clear();
252248
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
253-
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
254-
compiler_options.disable_main, compiler_options.symtab_only, infile,
255-
compiler_options.import_path);
249+
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
256250
std::cerr << diagnostics.render(input, lm, compiler_options);
257251
if (!r1.ok) {
258252
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -292,9 +286,7 @@ int emit_wat(const std::string &infile,
292286

293287
diagnostics.diagnostics.clear();
294288
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
295-
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
296-
compiler_options.disable_main, compiler_options.symtab_only, infile,
297-
compiler_options.import_path);
289+
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
298290
std::cerr << diagnostics.render(input, lm, compiler_options);
299291
if (!r1.ok) {
300292
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -337,9 +329,7 @@ int get_symbols (const std::string &infile,
337329
if (r1.ok) {
338330
LFortran::LPython::AST::ast_t* ast = r1.result;
339331
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
340-
x = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
341-
compiler_options.disable_main, compiler_options.symtab_only,
342-
infile, compiler_options.import_path);
332+
x = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
343333
if (!x.ok) {
344334
std::cout << "{}\n";
345335
return 0;
@@ -431,9 +421,7 @@ int get_errors (const std::string &infile,
431421
if (r1.ok) {
432422
LFortran::LPython::AST::ast_t* ast = r1.result;
433423
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
434-
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
435-
compiler_options.disable_main, compiler_options.symtab_only,
436-
infile, compiler_options.import_path);
424+
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
437425
}
438426
std::vector<LFortran::LPython::error_highlight> diag_lists;
439427
LFortran::LPython::error_highlight h;
@@ -544,9 +532,7 @@ int emit_llvm(const std::string &infile,
544532
LFortran::LPython::AST::ast_t* ast = r.result;
545533
diagnostics.diagnostics.clear();
546534
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
547-
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
548-
compiler_options.disable_main, compiler_options.symtab_only, infile,
549-
compiler_options.import_path);
535+
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
550536
std::cerr << diagnostics.render(input, lm, compiler_options);
551537
if (!r1.ok) {
552538
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -602,10 +588,9 @@ int compile_python_to_object_file(
602588
diagnostics.diagnostics.clear();
603589
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
604590
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
605-
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics,
606-
!(arg_c && compiler_options.disable_main),
607-
compiler_options.disable_main, compiler_options.symtab_only, infile,
608-
compiler_options.import_path);
591+
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options,
592+
!(arg_c && compiler_options.disable_main), infile);
593+
609594
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
610595
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
611596
std::cerr << diagnostics.render(input, lm, compiler_options);
@@ -687,9 +672,7 @@ int compile_to_binary_wasm(
687672
diagnostics.diagnostics.clear();
688673
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
689674
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
690-
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
691-
compiler_options.disable_main, compiler_options.symtab_only, infile,
692-
compiler_options.import_path);
675+
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
693676
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
694677
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
695678
std::cerr << diagnostics.render(input, lm, compiler_options);
@@ -754,8 +737,7 @@ int compile_to_binary_x86(
754737
diagnostics.diagnostics.clear();
755738
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
756739
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
757-
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
758-
compiler_options.disable_main, compiler_options.symtab_only, infile, compiler_options.import_path);
740+
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
759741
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
760742
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
761743
std::cerr << diagnostics.render(input, lm, compiler_options);
@@ -820,8 +802,7 @@ int compile_to_binary_wasm_to_x86(
820802
diagnostics.diagnostics.clear();
821803
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
822804
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
823-
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
824-
compiler_options.disable_main, compiler_options.symtab_only, infile, compiler_options.import_path);
805+
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
825806
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
826807
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
827808
std::cerr << diagnostics.render(input, lm, compiler_options);
@@ -1086,9 +1067,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_asr_from_source(char *input) {
10861067
if (ast.ok) {
10871068
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
10881069
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
1089-
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
1090-
compiler_options.disable_main, compiler_options.symtab_only, "input",
1091-
compiler_options.import_path);
1070+
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
10921071
out = diagnostics.render(input, lm, compiler_options);
10931072
if (asr.ok) {
10941073
out += LFortran::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent,
@@ -1106,9 +1085,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wat_from_source(char *input) {
11061085
if (ast.ok) {
11071086
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
11081087
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
1109-
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
1110-
compiler_options.disable_main, compiler_options.symtab_only, "input",
1111-
compiler_options.import_path);
1088+
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
11121089
out = diagnostics.render(input, lm, compiler_options);
11131090
if (asr.ok) {
11141091
LFortran::Result<LFortran::Vec<uint8_t>>
@@ -1135,9 +1112,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_cpp_from_source(char *input) {
11351112
if (ast.ok) {
11361113
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
11371114
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
1138-
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
1139-
compiler_options.disable_main, compiler_options.symtab_only, "input",
1140-
compiler_options.import_path);
1115+
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
11411116
out = diagnostics.render(input, lm, compiler_options);
11421117
if (asr.ok) {
11431118
auto res = LFortran::asr_to_cpp(al, *asr.result, diagnostics,
@@ -1175,9 +1150,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wasm_from_source(char *input) {
11751150
if (ast.ok) {
11761151
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
11771152
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
1178-
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
1179-
compiler_options.disable_main, compiler_options.symtab_only, "input",
1180-
compiler_options.import_path);
1153+
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
11811154
out = diagnostics.render(input, lm, compiler_options);
11821155
if (asr.ok) {
11831156
LFortran::Result<LFortran::Vec<uint8_t>>

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,17 @@ ASR::TranslationUnit_t* compile_module_till_asr(Allocator& al,
253253
// Convert the module from AST to ASR
254254
LFortran::LocationManager lm;
255255
lm.in_filename = infile;
256+
LFortran::CompilerOptions compiler_options;
257+
compiler_options.disable_main = true;
258+
compiler_options.symtab_only = false;
256259
Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr(al, *ast,
257-
diagnostics, false, true, false, infile, "");
260+
diagnostics, compiler_options, false, infile);
258261
// TODO: Uncomment once a check is added for ensuring
259262
// that module.py file hasn't changed between
260263
// builds.
261264
// save_pyc_files(*r2.result, infile + "c");
262265
std::string input;
263266
read_file(infile, input);
264-
CompilerOptions compiler_options;
265267
std::cerr << diagnostics.render(input, lm, compiler_options);
266268
if (!r2.ok) {
267269
LFORTRAN_ASSERT(diagnostics.has_error())
@@ -5679,17 +5681,16 @@ std::string get_parent_dir(const std::string &path) {
56795681
}
56805682

56815683
Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
5682-
AST::ast_t &ast, diag::Diagnostics &diagnostics, bool main_module,
5683-
bool disable_main, bool symtab_only, std::string file_path,
5684-
std::string import_path)
5684+
AST::ast_t &ast, diag::Diagnostics &diagnostics, CompilerOptions &compiler_options,
5685+
bool main_module, std::string file_path)
56855686
{
56865687
std::map<int, ASR::symbol_t*> ast_overload;
56875688
std::string parent_dir = get_parent_dir(file_path);
56885689
AST::Module_t *ast_m = AST::down_cast2<AST::Module_t>(&ast);
56895690

56905691
ASR::asr_t *unit;
56915692
auto res = symbol_table_visitor(al, *ast_m, diagnostics, main_module,
5692-
ast_overload, parent_dir, import_path);
5693+
ast_overload, parent_dir, compiler_options.import_path);
56935694
if (res.ok) {
56945695
unit = res.result;
56955696
} else {
@@ -5698,7 +5699,7 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
56985699
ASR::TranslationUnit_t *tu = ASR::down_cast2<ASR::TranslationUnit_t>(unit);
56995700
LFORTRAN_ASSERT(asr_verify(*tu));
57005701

5701-
if (!symtab_only) {
5702+
if (!compiler_options.symtab_only) {
57025703
auto res2 = body_visitor(al, *ast_m, diagnostics, unit, main_module,
57035704
ast_overload);
57045705
if (res2.ok) {
@@ -5712,7 +5713,7 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
57125713
if (main_module) {
57135714
// If it is a main module, turn it into a program
57145715
// Note: we can modify this behavior for interactive mode later
5715-
if (disable_main) {
5716+
if (compiler_options.disable_main) {
57165717
if (tu->n_items > 0) {
57175718
diagnostics.add(diag::Diagnostic(
57185719
"The script is invoked as the main module and it has code to execute,\n"

src/lpython/semantics/python_ast_to_asr.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ namespace LFortran::LPython {
99
std::string pickle_python(AST::ast_t &ast, bool colors=false, bool indent=false);
1010
std::string pickle_tree_python(AST::ast_t &ast, bool colors=true);
1111
Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
12-
LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics,
13-
bool main_module, bool disable_main, bool symtab_only, std::string file_path,
14-
std::string import_path);
12+
LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics, CompilerOptions &compiler_options,
13+
bool main_module, std::string file_path);
1514

1615
int save_pyc_files(const LFortran::ASR::TranslationUnit_t &u,
1716
std::string infile);

0 commit comments

Comments
 (0)