cleanup, support UnaryPlusNode in constant folding
authorStefan Behnel <scoder@users.berlios.de>
Fri, 12 Nov 2010 20:37:40 +0000 (21:37 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Fri, 12 Nov 2010 20:37:40 +0000 (21:37 +0100)
Cython/Compiler/Optimize.py

index 80f05b49a05c31625dd3f3071ec8f8cd6692c80d..460b6024ef3fc9ec1c51c1fff7223f37bcfb274c 100644 (file)
@@ -2940,17 +2940,23 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
                isinstance(node.operand, ExprNodes.IntNode) and node_type.is_pyobject:
             return ExprNodes.IntNode(node.pos, value = '-' + node.operand.value,
                                      type = node_type,
+                                     longness = node.operand.longness,
                                      constant_result = node.constant_result)
         return node
 
+    def visit_UnaryPlusNode(self, node):
+        self._calculate_const(node)
+        if node.constant_result is ExprNodes.not_a_constant:
+            return node
+        if node.constant_result == node.operand.constant_result:
+            return node.operand
+        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:
@@ -2966,14 +2972,8 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
         if node.constant_result is ExprNodes.not_a_constant:
             return node
         if isinstance(node.constant_result, float):
-            # We calculate float constants to make them available to
-            # the compiler, but we do not aggregate them into a
-            # constant node to prevent any loss of precision.
             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
 
         # now inject a new constant node with the calculated value