From fd94a2bd02b587a7f9726204693923dec214400b Mon Sep 17 00:00:00 2001 From: Vitja Makarov Date: Sun, 12 Dec 2010 17:18:19 +0300 Subject: [PATCH] Merge MarkClosureVisitor and MarkGeneratorVisitor --- Cython/Compiler/Main.py | 2 - Cython/Compiler/ParseTreeTransforms.py | 64 ++++++++++---------------- 2 files changed, 24 insertions(+), 42 deletions(-) diff --git a/Cython/Compiler/Main.py b/Cython/Compiler/Main.py index fbd32baf..13b0b49e 100644 --- a/Cython/Compiler/Main.py +++ b/Cython/Compiler/Main.py @@ -101,7 +101,6 @@ class Context(object): from ParseTreeTransforms import WithTransform, NormalizeTree, PostParse, PxdPostParse from ParseTreeTransforms import AnalyseDeclarationsTransform, AnalyseExpressionsTransform from ParseTreeTransforms import CreateClosureClasses, MarkClosureVisitor, DecoratorTransform - from ParseTreeTransforms import MarkGeneratorVisitor from ParseTreeTransforms import InterpretCompilerDirectives, TransformBuiltinMethods from ParseTreeTransforms import ExpandInplaceOperators from TypeInference import MarkAssignments, MarkOverflowingArithmetic @@ -134,7 +133,6 @@ class Context(object): InterpretCompilerDirectives(self, self.compiler_directives), _align_function_definitions, MarkClosureVisitor(self), - MarkGeneratorVisitor(self), ConstantFolding(), FlattenInListTransform(), WithTransform(self), diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py index d2185885..d0e4da9c 100644 --- a/Cython/Compiler/ParseTreeTransforms.py +++ b/Cython/Compiler/ParseTreeTransforms.py @@ -1301,39 +1301,7 @@ class AlignFunctionDefinitions(CythonTransform): # Enable this when internal def functions are allowed. # self.visitchildren(node) return node - - -class MarkClosureVisitor(CythonTransform): - - def visit_ModuleNode(self, node): - self.needs_closure = False - self.visitchildren(node) - return node - def visit_FuncDefNode(self, node): - self.needs_closure = False - self.visitchildren(node) - node.needs_closure = self.needs_closure - self.needs_closure = True - return node - - def visit_CFuncDefNode(self, node): - self.visit_FuncDefNode(node) - if node.needs_closure: - error(node.pos, "closures inside cdef functions not yet supported") - return node - - def visit_LambdaNode(self, node): - self.needs_closure = False - self.visitchildren(node) - node.needs_closure = self.needs_closure - self.needs_closure = True - return node - - def visit_ClassDefNode(self, node): - self.visitchildren(node) - self.needs_closure = True - return node class ClosureTempAllocator(object): def __init__(self, klass=None): @@ -1400,23 +1368,21 @@ class YieldNodeCollector(TreeVisitor): def visit_DefNode(self, node): pass -class MarkGeneratorVisitor(CythonTransform): - """XXX: merge me with MarkClosureVisitor""" - def __init__(self, context): - super(MarkGeneratorVisitor, self).__init__(context) +class MarkClosureVisitor(CythonTransform): def visit_ModuleNode(self, node): + self.needs_closure = False self.visitchildren(node) return node - def visit_ClassDefNode(self, node): + def visit_FuncDefNode(self, node): + self.needs_closure = False self.visitchildren(node) - return node + node.needs_closure = self.needs_closure + self.needs_closure = True - def visit_FuncDefNode(self, node): collector = YieldNodeCollector() collector.visitchildren(node) - self.visitchildren(node) if collector.yields: allocator = ClosureTempAllocator() @@ -1429,6 +1395,24 @@ class MarkGeneratorVisitor(CythonTransform): node.yields = collector.yields return node + def visit_CFuncDefNode(self, node): + self.visit_FuncDefNode(node) + if node.needs_closure: + error(node.pos, "closures inside cdef functions not yet supported") + return node + + def visit_LambdaNode(self, node): + self.needs_closure = False + self.visitchildren(node) + node.needs_closure = self.needs_closure + self.needs_closure = True + return node + + def visit_ClassDefNode(self, node): + self.visitchildren(node) + self.needs_closure = True + return node + class CreateClosureClasses(CythonTransform): # Output closure classes in module scope for all functions -- 2.26.2