Skip to content

Pass CompilerOptions as argument to python_ast_to_asr() #1273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 17 additions & 44 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ int emit_asr(const std::string &infile,

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

diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only, infile,
compiler_options.import_path);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -250,9 +246,7 @@ int emit_c(const std::string &infile,

diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only, infile,
compiler_options.import_path);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -292,9 +286,7 @@ int emit_wat(const std::string &infile,

diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only, infile,
compiler_options.import_path);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -337,9 +329,7 @@ int get_symbols (const std::string &infile,
if (r1.ok) {
LFortran::LPython::AST::ast_t* ast = r1.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
x = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only,
infile, compiler_options.import_path);
x = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
if (!x.ok) {
std::cout << "{}\n";
return 0;
Expand Down Expand Up @@ -431,9 +421,7 @@ int get_errors (const std::string &infile,
if (r1.ok) {
LFortran::LPython::AST::ast_t* ast = r1.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only,
infile, compiler_options.import_path);
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
}
std::vector<LFortran::LPython::error_highlight> diag_lists;
LFortran::LPython::error_highlight h;
Expand Down Expand Up @@ -544,9 +532,7 @@ int emit_llvm(const std::string &infile,
LFortran::LPython::AST::ast_t* ast = r.result;
diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only, infile,
compiler_options.import_path);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -602,10 +588,9 @@ int compile_python_to_object_file(
diagnostics.diagnostics.clear();
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics,
!(arg_c && compiler_options.disable_main),
compiler_options.disable_main, compiler_options.symtab_only, infile,
compiler_options.import_path);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options,
!(arg_c && compiler_options.disable_main), infile);

auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(input, lm, compiler_options);
Expand Down Expand Up @@ -687,9 +672,7 @@ int compile_to_binary_wasm(
diagnostics.diagnostics.clear();
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only, infile,
compiler_options.import_path);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(input, lm, compiler_options);
Expand Down Expand Up @@ -754,8 +737,7 @@ int compile_to_binary_x86(
diagnostics.diagnostics.clear();
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only, infile, compiler_options.import_path);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(input, lm, compiler_options);
Expand Down Expand Up @@ -820,8 +802,7 @@ int compile_to_binary_wasm_to_x86(
diagnostics.diagnostics.clear();
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only, infile, compiler_options.import_path);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(input, lm, compiler_options);
Expand Down Expand Up @@ -1086,9 +1067,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_asr_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only, "input",
compiler_options.import_path);
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
out = diagnostics.render(input, lm, compiler_options);
if (asr.ok) {
out += LFortran::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent,
Expand All @@ -1106,9 +1085,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wat_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only, "input",
compiler_options.import_path);
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
out = diagnostics.render(input, lm, compiler_options);
if (asr.ok) {
LFortran::Result<LFortran::Vec<uint8_t>>
Expand All @@ -1135,9 +1112,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_cpp_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only, "input",
compiler_options.import_path);
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
out = diagnostics.render(input, lm, compiler_options);
if (asr.ok) {
auto res = LFortran::asr_to_cpp(al, *asr.result, diagnostics,
Expand Down Expand Up @@ -1175,9 +1150,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wasm_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, true,
compiler_options.disable_main, compiler_options.symtab_only, "input",
compiler_options.import_path);
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
out = diagnostics.render(input, lm, compiler_options);
if (asr.ok) {
LFortran::Result<LFortran::Vec<uint8_t>>
Expand Down
17 changes: 9 additions & 8 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,17 @@ ASR::TranslationUnit_t* compile_module_till_asr(Allocator& al,
// Convert the module from AST to ASR
LFortran::LocationManager lm;
lm.in_filename = infile;
LFortran::CompilerOptions compiler_options;
compiler_options.disable_main = true;
compiler_options.symtab_only = false;
Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr(al, *ast,
diagnostics, false, true, false, infile, "");
diagnostics, compiler_options, false, infile);
// TODO: Uncomment once a check is added for ensuring
// that module.py file hasn't changed between
// builds.
// save_pyc_files(*r2.result, infile + "c");
std::string input;
read_file(infile, input);
CompilerOptions compiler_options;
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r2.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -5679,17 +5681,16 @@ std::string get_parent_dir(const std::string &path) {
}

Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
AST::ast_t &ast, diag::Diagnostics &diagnostics, bool main_module,
bool disable_main, bool symtab_only, std::string file_path,
std::string import_path)
AST::ast_t &ast, diag::Diagnostics &diagnostics, CompilerOptions &compiler_options,
bool main_module, std::string file_path)
{
std::map<int, ASR::symbol_t*> ast_overload;
std::string parent_dir = get_parent_dir(file_path);
AST::Module_t *ast_m = AST::down_cast2<AST::Module_t>(&ast);

ASR::asr_t *unit;
auto res = symbol_table_visitor(al, *ast_m, diagnostics, main_module,
ast_overload, parent_dir, import_path);
ast_overload, parent_dir, compiler_options.import_path);
if (res.ok) {
unit = res.result;
} else {
Expand All @@ -5698,7 +5699,7 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
ASR::TranslationUnit_t *tu = ASR::down_cast2<ASR::TranslationUnit_t>(unit);
LFORTRAN_ASSERT(asr_verify(*tu));

if (!symtab_only) {
if (!compiler_options.symtab_only) {
auto res2 = body_visitor(al, *ast_m, diagnostics, unit, main_module,
ast_overload);
if (res2.ok) {
Expand All @@ -5712,7 +5713,7 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
if (main_module) {
// If it is a main module, turn it into a program
// Note: we can modify this behavior for interactive mode later
if (disable_main) {
if (compiler_options.disable_main) {
if (tu->n_items > 0) {
diagnostics.add(diag::Diagnostic(
"The script is invoked as the main module and it has code to execute,\n"
Expand Down
5 changes: 2 additions & 3 deletions src/lpython/semantics/python_ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ namespace LFortran::LPython {
std::string pickle_python(AST::ast_t &ast, bool colors=false, bool indent=false);
std::string pickle_tree_python(AST::ast_t &ast, bool colors=true);
Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics,
bool main_module, bool disable_main, bool symtab_only, std::string file_path,
std::string import_path);
LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics, CompilerOptions &compiler_options,
bool main_module, std::string file_path);

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