From f93f21363ad714590309f15b4a4236226c588601 Mon Sep 17 00:00:00 2001 From: Smit Lunagariya Date: Sun, 30 Jul 2023 13:03:46 +0530 Subject: [PATCH] Revert "ASR: Support chained CompareOp" --- integration_tests/CMakeLists.txt | 2 - integration_tests/expr_22.py | 22 --- integration_tests/structs_35.py | 32 ---- src/libasr/codegen/asr_to_c.cpp | 11 +- src/libasr/codegen/asr_to_c_cpp.h | 3 - src/lpython/semantics/python_ast_to_asr.cpp | 181 ++++++++------------ 6 files changed, 72 insertions(+), 179 deletions(-) delete mode 100644 integration_tests/expr_22.py delete mode 100644 integration_tests/structs_35.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 51a3f667ab..aac3e60aed 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -472,7 +472,6 @@ RUN(NAME expr_18 FAIL LABELS cpython llvm c) RUN(NAME expr_19 LABELS cpython llvm c) RUN(NAME expr_20 LABELS cpython llvm c) RUN(NAME expr_21 LABELS cpython llvm c) -RUN(NAME expr_22 LABELS cpython llvm c) RUN(NAME expr_01u LABELS cpython llvm c NOFAST) RUN(NAME expr_02u LABELS cpython llvm c NOFAST) @@ -672,7 +671,6 @@ RUN(NAME structs_31 LABELS cpython llvm c) RUN(NAME structs_32 LABELS cpython llvm c) RUN(NAME structs_33 LABELS cpython llvm c) RUN(NAME structs_34 LABELS cpython llvm c) -RUN(NAME structs_35 LABELS cpython llvm c) RUN(NAME symbolics_01 LABELS cpython_sym c_sym) RUN(NAME symbolics_02 LABELS cpython_sym c_sym) diff --git a/integration_tests/expr_22.py b/integration_tests/expr_22.py deleted file mode 100644 index d59fee6941..0000000000 --- a/integration_tests/expr_22.py +++ /dev/null @@ -1,22 +0,0 @@ -from lpython import i32 - -def f(): - x: i32 = 2 - y: i32 = 1 - z: i32 = 1 - t: i32 = 1 - assert x > y == z - assert not (x == y == z) - assert y == z == t != x - assert x > y == z >= t - t = 0 - assert x > y == z >= t - t = 4 - assert not (x > y == z >= t) - assert t > x > y == z - assert 3 > 2 >= 0 <= 6 - assert t > y < x - assert not (2 == 3 > 4) - - -f() diff --git a/integration_tests/structs_35.py b/integration_tests/structs_35.py deleted file mode 100644 index ed1ce302a2..0000000000 --- a/integration_tests/structs_35.py +++ /dev/null @@ -1,32 +0,0 @@ -from lpython import (i8, i32, i64, f32, f64, - dataclass - ) -from numpy import (empty, - int8, - ) - -# test issue 2131 - -@dataclass -class Foo: - a : i8[4] = empty(4, dtype=int8) - dim : i32 = 4 - -def trinary_majority(x : Foo, y : Foo, z : Foo) -> Foo: - foo : Foo = Foo() - - assert foo.dim == x.dim == y.dim == z.dim - - return foo - - -t1 : Foo = Foo() -t1.a = empty(4, dtype=int8) - -t2 : Foo = Foo() -t2.a = empty(4, dtype=int8) - -t3 : Foo = Foo() -t3.a = empty(4, dtype=int8) - -r1 : Foo = trinary_majority(t1, t2, t3) diff --git a/src/libasr/codegen/asr_to_c.cpp b/src/libasr/codegen/asr_to_c.cpp index 89b2c95d34..f25d565c46 100644 --- a/src/libasr/codegen/asr_to_c.cpp +++ b/src/libasr/codegen/asr_to_c.cpp @@ -583,8 +583,6 @@ class ASRToCVisitor : public BaseCCPPVisitor std::string unit_src = ""; indentation_level = 0; indentation_spaces = 4; - SymbolTable* current_scope_copy = current_scope; - current_scope = global_scope; c_ds_api->set_indentation(indentation_level, indentation_spaces); c_ds_api->set_global_scope(global_scope); c_utils_functions->set_indentation(indentation_level, indentation_spaces); @@ -762,7 +760,6 @@ R"( out_file.close(); } } - current_scope = current_scope_copy; } void visit_Module(const ASR::Module_t &x) { @@ -771,8 +768,7 @@ R"( } else { intrinsic_module = false; } - SymbolTable *current_scope_copy = current_scope; - current_scope = x.m_symtab; + std::string unit_src = ""; for (auto &item : x.m_symtab->get_scope()) { if (ASR::is_a(*item.second)) { @@ -817,15 +813,13 @@ R"( } src = unit_src; intrinsic_module = false; - current_scope = current_scope_copy; } void visit_Program(const ASR::Program_t &x) { // Topologically sort all program functions // and then define them in the right order std::vector func_order = ASRUtils::determine_function_definition_order(x.m_symtab); - SymbolTable *current_scope_copy = current_scope; - current_scope = x.m_symtab; + // Generate code for nested subroutines and functions first: std::string contains; for (auto &item : func_order) { @@ -904,7 +898,6 @@ R"( // Initialise Numpy + decl + body + indent1 + "return 0;\n}\n"; indentation_level -= 2; - current_scope = current_scope_copy; } template diff --git a/src/libasr/codegen/asr_to_c_cpp.h b/src/libasr/codegen/asr_to_c_cpp.h index 11f3591eba..4969740a29 100644 --- a/src/libasr/codegen/asr_to_c_cpp.h +++ b/src/libasr/codegen/asr_to_c_cpp.h @@ -193,8 +193,6 @@ class BaseCCPPVisitor : public ASR::BaseVisitor void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { global_scope = x.m_global_scope; - SymbolTable* current_scope_copy = current_scope; - current_scope = global_scope; // All loose statements must be converted to a function, so the items // must be empty: LCOMPILERS_ASSERT(x.n_items == 0); @@ -257,7 +255,6 @@ R"(#include } src = unit_src; - current_scope = current_scope_copy; } std::string check_tmp_buffer() { diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index aa0db8e46a..987900e57d 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -6064,71 +6064,54 @@ class BodyVisitor : public CommonVisitor { body.size()); } - void compare_helper(const Location &loc, AST::expr_t *m_left, AST::expr_t *m_right, ASR::cmpopType asr_op) { - this->visit_expr(*m_left); + void visit_Compare(const AST::Compare_t &x) { + this->visit_expr(*x.m_left); ASR::expr_t *left = ASRUtils::EXPR(tmp); - this->visit_expr(*m_right); + if (x.n_comparators > 1) { + diag.add(diag::Diagnostic( + "Only one comparison operator is supported for now", + diag::Level::Error, diag::Stage::Semantic, { + diag::Label("multiple comparison operators", + {x.m_comparators[0]->base.loc}) + }) + ); + throw SemanticAbort(); + } + this->visit_expr(*x.m_comparators[0]); ASR::expr_t *right = ASRUtils::EXPR(tmp); - ASR::ttype_t *type = ASRUtils::TYPE( - ASR::make_Logical_t(al, loc, 4)); - ASR::expr_t *value = nullptr; + + ASR::cmpopType asr_op; + switch (x.m_ops) { + case (AST::cmpopType::Eq): { asr_op = ASR::cmpopType::Eq; break; } + case (AST::cmpopType::Gt): { asr_op = ASR::cmpopType::Gt; break; } + case (AST::cmpopType::GtE): { asr_op = ASR::cmpopType::GtE; break; } + case (AST::cmpopType::Lt): { asr_op = ASR::cmpopType::Lt; break; } + case (AST::cmpopType::LtE): { asr_op = ASR::cmpopType::LtE; break; } + case (AST::cmpopType::NotEq): { asr_op = ASR::cmpopType::NotEq; break; } + default: { + throw SemanticError("Comparison operator not implemented", + x.base.base.loc); + } + } + ASR::ttype_t *left_type = ASRUtils::expr_type(left); ASR::ttype_t *right_type = ASRUtils::expr_type(right); - ASR::ttype_t *dest_type = left_type; - ASR::expr_t *overloaded = nullptr; if( ASR::is_a(*left_type) ) { left_type = ASRUtils::get_contained_type(left_type); } if( ASR::is_a(*right_type) ) { right_type = ASRUtils::get_contained_type(right_type); } + ASR::expr_t *overloaded = nullptr; if (!ASRUtils::is_logical(*left_type) || !ASRUtils::is_logical(*right_type)) { cast_helper(left, right, false); } + left_type = ASRUtils::expr_type(left); + right_type = ASRUtils::expr_type(right); + ASR::ttype_t *dest_type = left_type; if (!ASRUtils::check_equal_type(left_type, right_type)) { - if (AST::is_a(*m_left)) { - // handle chained comparisons - LCOMPILERS_ASSERT(ASRUtils::is_logical(*left_type)); - AST::Compare_t *lc = AST::down_cast(m_left); - compare_helper(loc, lc->m_comparators[0], m_right, asr_op); - right = ASRUtils::EXPR(tmp); - right_type = ASRUtils::expr_type(right); - LCOMPILERS_ASSERT(ASRUtils::is_logical(*right_type)); - if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) { - bool left_value = ASR::down_cast( - ASRUtils::expr_value(left))->m_value; - bool right_value = ASR::down_cast( - ASRUtils::expr_value(right))->m_value; - bool result = left_value && right_value; - value = ASR::down_cast(ASR::make_LogicalConstant_t( - al, loc, result, type)); - } - tmp = ASR::make_LogicalBinOp_t(al, loc, left, - ASR::logicalbinopType::And, right, type, value); - return; - } else if (AST::is_a(*m_right)) { - // handle chained comparisons - LCOMPILERS_ASSERT(ASRUtils::is_logical(*right_type)); - AST::Compare_t *rc = AST::down_cast(m_right); - compare_helper(loc, m_left, rc->m_left, asr_op); - left = ASRUtils::EXPR(tmp); - left_type = ASRUtils::expr_type(left); - LCOMPILERS_ASSERT(ASRUtils::is_logical(*left_type)); - if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) { - bool left_value = ASR::down_cast( - ASRUtils::expr_value(left))->m_value; - bool right_value = ASR::down_cast( - ASRUtils::expr_value(right))->m_value; - bool result = left_value && right_value; - value = ASR::down_cast(ASR::make_LogicalConstant_t( - al, loc, result, type)); - } - tmp = ASR::make_LogicalBinOp_t(al, loc, left, - ASR::logicalbinopType::And, right, type, value); - return; - } std::string ltype = ASRUtils::type_to_str_python(ASRUtils::expr_type(left)); std::string rtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(right)); diag.add(diag::Diagnostic( @@ -6140,25 +6123,30 @@ class BodyVisitor : public CommonVisitor { ); throw SemanticAbort(); } + ASR::ttype_t *type = ASRUtils::TYPE( + ASR::make_Logical_t(al, x.base.base.loc, 4)); + ASR::expr_t *value = nullptr; + if( ASR::is_a(*dest_type) || ASR::is_a(*dest_type) ) { dest_type = ASRUtils::get_contained_type(dest_type); } + if (ASRUtils::is_array(dest_type)) { ASR::dimension_t* m_dims = nullptr; int n_dims = ASRUtils::extract_dimensions_from_ttype(dest_type, m_dims); int array_size = ASRUtils::get_fixed_size_of_array(m_dims, n_dims); if (array_size == -1) { - throw SemanticError("The truth value of an array is ambiguous. Use a.any() or a.all()", loc); + throw SemanticError("The truth value of an array is ambiguous. Use a.any() or a.all()", x.base.base.loc); } else if (array_size != 1) { - throw SemanticError("The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()", loc); + throw SemanticError("The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()", x.base.base.loc); } else { Vec argsL, argsR; argsL.reserve(al, 1); argsR.reserve(al, 1); for (int i = 0; i < n_dims; i++) { ASR::array_index_t aiL, aiR; - ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4)); - ASR::expr_t* const_zero = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc, 0, int_type)); + ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 4)); + ASR::expr_t* const_zero = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, x.base.base.loc, 0, int_type)); aiL.m_right = aiR.m_right = const_zero; aiL.m_left = aiR.m_left = nullptr; aiL.m_step = aiR.m_step = nullptr; @@ -6168,10 +6156,8 @@ class BodyVisitor : public CommonVisitor { argsR.push_back(al, aiR); } dest_type = ASRUtils::type_get_past_array(dest_type); - left = ASRUtils::EXPR(make_ArrayItem_t(al, left->base.loc, - left, argsL.p, argsL.n, dest_type, ASR::arraystorageType::RowMajor, nullptr)); - right = ASRUtils::EXPR(make_ArrayItem_t(al, right->base.loc, - right, argsR.p, argsR.n, dest_type, ASR::arraystorageType::RowMajor, nullptr)); + left = ASRUtils::EXPR(make_ArrayItem_t(al, left->base.loc, left, argsL.p, argsL.n, dest_type, ASR::arraystorageType::RowMajor, nullptr)); + right = ASRUtils::EXPR(make_ArrayItem_t(al, right->base.loc, right, argsR.p, argsR.n, dest_type, ASR::arraystorageType::RowMajor, nullptr)); } } @@ -6191,13 +6177,13 @@ class BodyVisitor : public CommonVisitor { case (ASR::cmpopType::NotEq): { result = left_value != right_value; break; } default: { throw SemanticError("Comparison operator not implemented", - loc); + x.base.base.loc); } } value = ASR::down_cast(ASR::make_LogicalConstant_t( - al, loc, result, type)); + al, x.base.base.loc, result, type)); } - tmp = ASR::make_IntegerCompare_t(al, loc, left, asr_op, right, type, value); + tmp = ASR::make_IntegerCompare_t(al, x.base.base.loc, left, asr_op, right, type, value); } else if (ASRUtils::is_unsigned_integer(*dest_type)) { if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) { int64_t left_value = -1; @@ -6214,13 +6200,13 @@ class BodyVisitor : public CommonVisitor { case (ASR::cmpopType::NotEq): { result = left_value != right_value; break; } default: { throw SemanticError("Comparison operator not implemented", - loc); + x.base.base.loc); } } value = ASR::down_cast(ASR::make_LogicalConstant_t( - al, loc, result, type)); + al, x.base.base.loc, result, type)); } - tmp = ASR::make_UnsignedIntegerCompare_t(al, loc, left, asr_op, right, type, value); + tmp = ASR::make_UnsignedIntegerCompare_t(al, x.base.base.loc, left, asr_op, right, type, value); } else if (ASRUtils::is_real(*dest_type)) { if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) { @@ -6238,14 +6224,14 @@ class BodyVisitor : public CommonVisitor { case (ASR::cmpopType::NotEq): { result = left_value != right_value; break; } default: { throw SemanticError("Comparison operator not implemented", - loc); + x.base.base.loc); } } value = ASR::down_cast(ASR::make_LogicalConstant_t( - al, loc, result, type)); + al, x.base.base.loc, result, type)); } - tmp = ASR::make_RealCompare_t(al, loc, left, asr_op, right, type, value); + tmp = ASR::make_RealCompare_t(al, x.base.base.loc, left, asr_op, right, type, value); } else if (ASRUtils::is_complex(*dest_type)) { @@ -6271,14 +6257,14 @@ class BodyVisitor : public CommonVisitor { default: { throw SemanticError("'" + ASRUtils::cmpop_to_str(asr_op) + "' comparison is not supported between complex numbers", - loc); + x.base.base.loc); } } value = ASR::down_cast(ASR::make_LogicalConstant_t( - al, loc, result, type)); + al, x.base.base.loc, result, type)); } - tmp = ASR::make_ComplexCompare_t(al, loc, left, asr_op, right, type, value); + tmp = ASR::make_ComplexCompare_t(al, x.base.base.loc, left, asr_op, right, type, value); } else if (ASRUtils::is_logical(*dest_type)) { @@ -6297,14 +6283,14 @@ class BodyVisitor : public CommonVisitor { case (ASR::cmpopType::NotEq): { result = left_value != right_value; break; } default: { throw SemanticError("Comparison operator not implemented", - loc); + x.base.base.loc); } } value = ASR::down_cast(ASR::make_LogicalConstant_t( - al, loc, result, type)); + al, x.base.base.loc, result, type)); } - tmp = ASR::make_LogicalCompare_t(al, loc, left, asr_op, right, type, value); + tmp = ASR::make_LogicalCompare_t(al, x.base.base.loc, left, asr_op, right, type, value); } else if (ASRUtils::is_character(*dest_type)) { @@ -6343,77 +6329,50 @@ class BodyVisitor : public CommonVisitor { break; } default: { - throw SemanticError("ICE: Unknown compare operator", loc); // should never happen + throw SemanticError("ICE: Unknown compare operator", x.base.base.loc); // should never happen } } value = ASR::down_cast(ASR::make_LogicalConstant_t( - al, loc, result, type)); + al, x.base.base.loc, result, type)); } - tmp = ASR::make_StringCompare_t(al, loc, left, asr_op, right, type, value); + tmp = ASR::make_StringCompare_t(al, x.base.base.loc, left, asr_op, right, type, value); } else if (ASR::is_a(*dest_type)) { if (asr_op != ASR::cmpopType::Eq && asr_op != ASR::cmpopType::NotEq && asr_op != ASR::cmpopType::Lt && asr_op != ASR::cmpopType::LtE && asr_op != ASR::cmpopType::Gt && asr_op != ASR::cmpopType::GtE) { throw SemanticError("Only ==, !=, <, <=, >, >= operators " "are supported for Tuples", - loc); + x.base.base.loc); } - tmp = ASR::make_TupleCompare_t(al, loc, left, asr_op, right, type, value); + tmp = ASR::make_TupleCompare_t(al, x.base.base.loc, left, asr_op, right, type, value); } else if (ASR::is_a(*dest_type)) { if (asr_op != ASR::cmpopType::Eq && asr_op != ASR::cmpopType::NotEq && asr_op != ASR::cmpopType::Lt && asr_op != ASR::cmpopType::LtE && asr_op != ASR::cmpopType::Gt && asr_op != ASR::cmpopType::GtE) { throw SemanticError("Only ==, !=, <, <=, >, >= operators " "are supported for Lists", - loc); + x.base.base.loc); } - tmp = ASR::make_ListCompare_t(al, loc, left, asr_op, right, type, value); + tmp = ASR::make_ListCompare_t(al, x.base.base.loc, left, asr_op, right, type, value); } else if (ASR::is_a(*dest_type)) { if (asr_op != ASR::cmpopType::Eq && asr_op != ASR::cmpopType::NotEq) { throw SemanticError("Only Equal and Not-equal operators are supported for CPtr", - loc); + x.base.base.loc); } - tmp = ASR::make_CPtrCompare_t(al, loc, left, asr_op, right, type, value); + tmp = ASR::make_CPtrCompare_t(al, x.base.base.loc, left, asr_op, right, type, value); } else if (ASR::is_a(*dest_type)) { - tmp = ASR::make_SymbolicCompare_t(al, loc, left, asr_op, right, type, value); + tmp = ASR::make_SymbolicCompare_t(al, x.base.base.loc, left, asr_op, right, type, value); } else { throw SemanticError("Compare not supported for type: " + ASRUtils::type_to_str_python(dest_type), - loc); + x.base.base.loc); } if (overloaded != nullptr) { - tmp = ASR::make_OverloadedCompare_t(al, loc, left, asr_op, right, type, + tmp = ASR::make_OverloadedCompare_t(al, x.base.base.loc, left, asr_op, right, type, value, overloaded); } } - void visit_Compare(const AST::Compare_t &x) { - if (x.n_comparators > 1) { - diag.add(diag::Diagnostic( - "Only one comparison operator is supported for now", - diag::Level::Error, diag::Stage::Semantic, { - diag::Label("multiple comparison operators", - {x.m_comparators[0]->base.loc}) - }) - ); - throw SemanticAbort(); - } - - ASR::cmpopType asr_op; - switch (x.m_ops) { - case (AST::cmpopType::Eq): { asr_op = ASR::cmpopType::Eq; break; } - case (AST::cmpopType::Gt): { asr_op = ASR::cmpopType::Gt; break; } - case (AST::cmpopType::GtE): { asr_op = ASR::cmpopType::GtE; break; } - case (AST::cmpopType::Lt): { asr_op = ASR::cmpopType::Lt; break; } - case (AST::cmpopType::LtE): { asr_op = ASR::cmpopType::LtE; break; } - case (AST::cmpopType::NotEq): { asr_op = ASR::cmpopType::NotEq; break; } - default: { - throw SemanticError("Comparison operator not implemented", - x.base.base.loc); - } - } - compare_helper(x.base.base.loc, x.m_left, x.m_comparators[0], asr_op); - } void visit_ConstantEllipsis(const AST::ConstantEllipsis_t &/*x*/) { tmp = nullptr;