compile classes in Visitor.py into real extension classes
authorStefan Behnel <scoder@users.berlios.de>
Sun, 21 Dec 2008 09:05:12 +0000 (10:05 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Sun, 21 Dec 2008 09:05:12 +0000 (10:05 +0100)
Cython/Compiler/Optimize.py
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/Visitor.pxd [new file with mode: 0644]
Cython/Compiler/Visitor.py

index ea3d19b147e4add455b62585b752eb86d7d121b7..2c16e7114d441427432c22ef870945b2b0e3c1f9 100644 (file)
@@ -49,10 +49,7 @@ class IterationTransform(Visitor.VisitorTransform):
     PyDict_Next_entry = Symtab.Entry(
         PyDict_Next_name, PyDict_Next_name, PyDict_Next_func_type)
 
-    def visit_Node(self, node):
-        # descend into statements (loops) and nodes (comprehensions)
-        self.visitchildren(node)
-        return node
+    visit_Node = Visitor.VisitorTransform.recurse_to_children
 
     def visit_ModuleNode(self, node):
         self.current_scope = node.scope
@@ -361,10 +358,7 @@ class SwitchTransform(Visitor.VisitorTransform):
                                     cases = cases,
                                     else_clause = node.else_clause)
 
-
-    def visit_Node(self, node):
-        self.visitchildren(node)
-        return node
+    visit_Node = Visitor.VisitorTransform.recurse_to_children
                               
 
 class FlattenInListTransform(Visitor.VisitorTransform, SkipDeclarations):
@@ -417,9 +411,7 @@ class FlattenInListTransform(Visitor.VisitorTransform, SkipDeclarations):
         condition = reduce(concat, conds)
         return UtilNodes.EvalWithTempExprNode(lhs, condition)
 
-    def visit_Node(self, node):
-        self.visitchildren(node)
-        return node
+    visit_Node = Visitor.VisitorTransform.recurse_to_children
 
 
 class FlattenBuiltinTypeCreation(Visitor.VisitorTransform):
@@ -534,9 +526,7 @@ class FlattenBuiltinTypeCreation(Visitor.VisitorTransform):
             return node
         return node.arg
 
-    def visit_Node(self, node):
-        self.visitchildren(node)
-        return node
+    visit_Node = Visitor.VisitorTransform.recurse_to_children
 
 
 class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
@@ -614,9 +604,7 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
         self.current_scope = old_scope
         return node
 
-    def visit_Node(self, node):
-        self.visitchildren(node)
-        return node
+    visit_Node = Visitor.VisitorTransform.recurse_to_children
 
 
 class FinalOptimizePhase(Visitor.CythonTransform):
index 2e7bb0613c3251a8ea5f57151e8d01e95bd23b3e..50a61602ba3e878d0752fa0304086ccdc58a2b92 100644 (file)
@@ -21,9 +21,7 @@ class NameNodeCollector(TreeVisitor):
         super(NameNodeCollector, self).__init__()
         self.name_nodes = []
 
-    def visit_Node(self, node):
-        self.visitchildren(node)
-        return node
+    visit_Node = TreeVisitor.visitchildren
 
     def visit_NameNode(self, node):
         self.name_nodes.append(node)
@@ -422,10 +420,6 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
         else:
             node.cython_attribute = self.option_names.get(node.name)
         return node
-        
-    def visit_Node(self, node):
-        self.visitchildren(node)
-        return node
 
     def try_to_parse_option(self, node):
         # If node is the contents of an option (in a with statement or
@@ -595,10 +589,7 @@ class ComprehensionTransform(VisitorTransform):
         self.visitchildren(node)
         return node
 
-    def visit_Node(self, node):
-        # descend into statements (loops) and nodes (comprehensions)
-        self.visitchildren(node)
-        return node
+    visit_Node = VisitorTransform.recurse_to_children
 
     def visit_ComprehensionNode(self, node):
         if type(node.loop) not in (Nodes.ForInStatNode,
diff --git a/Cython/Compiler/Visitor.pxd b/Cython/Compiler/Visitor.pxd
new file mode 100644 (file)
index 0000000..9f83c14
--- /dev/null
@@ -0,0 +1,15 @@
+cdef class BasicVisitor:
+    cdef object dispatch_table
+    cpdef visit(self, obj)
+
+cdef class TreeVisitor(BasicVisitor):
+    cdef public access_path
+    cpdef visitchild(self, child, parent, attrname, idx)
+
+cdef class VisitorTransform(TreeVisitor):
+    cpdef visitchildren(self, parent, attrs=*)
+    cpdef recurse_to_children(self, node)
+
+cdef class CythonTransform(VisitorTransform):
+    cdef public context
+    cdef public current_directives
index cf65c1ae22ef8439f7fcbc279d96e54894206ca8..5c89e797de6a774c1601a16205e8fd94b0aad340 100644 (file)
@@ -143,7 +143,8 @@ class VisitorTransform(TreeVisitor):
     are within a StatListNode or similar before doing this.)
     """
     def visitchildren(self, parent, attrs=None):
-        result = super(VisitorTransform, self).visitchildren(parent, attrs)
+#        result = super(VisitorTransform, self).visitchildren(parent, attrs)
+        result = TreeVisitor.visitchildren(self, parent, attrs)
         for attr, newnode in result.iteritems():
             if not isinstance(newnode, list):
                 setattr(parent, attr, newnode)
@@ -158,6 +159,10 @@ class VisitorTransform(TreeVisitor):
                             newlist.append(x)
                 setattr(parent, attr, newlist)
         return result        
+
+    def recurse_to_children(self, node):
+        self.visitchildren(node)
+        return node
     
     def __call__(self, root):
         return self.visit(root)