collapse BoolBinopNode during constant folding, small fix for BoolNode folding
authorStefan Behnel <scoder@users.berlios.de>
Tue, 4 May 2010 19:11:33 +0000 (21:11 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Tue, 4 May 2010 19:11:33 +0000 (21:11 +0200)
Cython/Compiler/Optimize.py
tests/run/bint_binop_T145.pyx

index 3a36989d9086c0ce54fdfcf4c3bf6be97687469a..35ae0ac572612b3369bf3ed6b2f4919a7ce0a48a 100644 (file)
@@ -2526,6 +2526,24 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
         self._calculate_const(node)
         return node
 
+    def visit_BoolBinopNode(self, node):
+        self._calculate_const(node)
+        if node.constant_result is ExprNodes.not_a_constant:
+            return node
+        if not node.operand1.is_literal or not node.operand2.is_literal:
+            # We calculate other constants to make them available to
+            # the compiler, but we only aggregate constant nodes
+            # recursively, so non-const nodes are straight out.
+            return node
+
+        if node.constant_result == node.operand1.constant_result and node.operand1.is_literal:
+            return node.operand1
+        elif node.constant_result == node.operand2.constant_result and node.operand2.is_literal:
+            return node.operand2
+        else:
+            # FIXME: we could do more ...
+            return node
+
     def visit_BinopNode(self, node):
         self._calculate_const(node)
         if node.constant_result is ExprNodes.not_a_constant:
@@ -2568,7 +2586,10 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
                 new_node = target_class(pos=node.pos, type = widest_type)
 
         new_node.constant_result = node.constant_result
-        new_node.value = str(node.constant_result)
+        if isinstance(node, ExprNodes.BoolNode):
+            new_node.value = node.constant_result
+        else:
+            new_node.value = str(node.constant_result)
         #new_node = new_node.coerce_to(node.type, self.current_scope)
         return new_node
 
index c23232a830dbef51865574d998c67d3251e686ef..2717272ed41b0deb5dc6615a093c0ab262dc2299 100644 (file)
@@ -1,4 +1,7 @@
 
+cimport cython
+
+@cython.test_fail_if_path_exists('//BoolBinopNode')
 def or_literal_bint():
     """
     >>> True or 5
@@ -8,6 +11,7 @@ def or_literal_bint():
     """
     return True or 5
 
+@cython.test_fail_if_path_exists('//BoolBinopNode')
 def and_literal_bint():
     """
     >>> 5 and True
@@ -17,6 +21,7 @@ def and_literal_bint():
     """
     return 5 and True
 
+@cython.test_fail_if_path_exists('//BoolBinopNode')
 def False_and_True_or_0():
     """
     >>> False and True or 0
@@ -26,6 +31,7 @@ def False_and_True_or_0():
     """
     return False and True or 0
 
+@cython.test_fail_if_path_exists('//BoolBinopNode')
 def True_and_True_or_0():
     """
     >>> True and True or 0