# interned_cname string
is_name = 1
+ skip_assignment_decref = False
def create_analysed_rvalue(pos, env, entry):
node = NameNode(pos)
rhs.generate_disposal_code(code)
else:
- if self.type.is_pyobject:
+ if self.type.is_pyobject and not self.skip_assignment_decref:
#print "NameNode.generate_assignment_code: to", self.name ###
#print "...from", rhs ###
#print "...LHS type", self.type, "ctype", self.ctype() ###
from ParseTreeTransforms import WithTransform, NormalizeTree, PostParse
from ParseTreeTransforms import AnalyseDeclarationsTransform, AnalyseExpressionsTransform
from ParseTreeTransforms import CreateClosureClasses, MarkClosureVisitor, DecoratorTransform
- from Optimize import FlattenInListTransform, SwitchTransform
+ from Optimize import FlattenInListTransform, SwitchTransform, OptimizeRefcounting
from Buffer import BufferTransform
from ModuleNode import check_c_classes
check_c_classes,
AnalyseExpressionsTransform(context),
BufferTransform(context),
- SwitchTransform(),
+ SwitchTransform(),
+ OptimizeRefcounting(context),
# CreateClosureClasses(context),
create_generate_code(context, options, result)
]
#
# lhs ExprNode Left hand side
# rhs ExprNode Right hand side
+ # first bool Is this guaranteed the first assignment to lhs?
child_attrs = ["lhs", "rhs"]
+ first = False
def analyse_declarations(self, env):
self.lhs.analyse_target_declaration(env)
# self.lhs.allocate_target_temps(env)
# self.lhs.release_target_temp(env)
# self.rhs.release_temp(env)
-
+
def generate_rhs_evaluation_code(self, code):
self.rhs.generate_evaluation_code(code)
def visit_Node(self, node):
self.visitchildren(node)
return node
+
+
+class OptimizeRefcounting(Visitor.CythonTransform):
+ def visit_SingleAssignmentNode(self, node):
+ if node.first:
+ lhs = node.lhs
+ if isinstance(lhs, ExprNodes.NameNode) and lhs.entry.type.is_pyobject:
+ # Have variable initialized to 0 rather than None
+ lhs.entry.init_to_none = False
+ lhs.entry.init = 0
+ # Set a flag in NameNode to skip the decref
+ lhs.skip_assignment_decref = True
+ return node
raise PostParseError(decl.pos, ERR_CDEF_INCLASS)
stats.append(SingleAssignmentNode(node.pos,
lhs=NameNode(node.pos, name=decl.name),
- rhs=decl.default))
+ rhs=decl.default, first=True))
decl.default = None
return stats