# HG changeset patch # User paulb@jeremy # Date 1154559767 -7200 # Node ID cc6abe59ef053461732cb32349e00bdbbc71421f # Parent b44426480e07a4fc9fbb3a4b1d96a523081dbf69 Fixed temporary value manipulation in annotate. Improved invocation support, adding star, dstar and missing args attributes to Invoke nodes. diff -r b44426480e07 -r cc6abe59ef05 annotate.py --- a/annotate.py Thu Aug 03 01:00:40 2006 +0200 +++ b/annotate.py Thu Aug 03 01:02:47 2006 +0200 @@ -162,8 +162,6 @@ def __init__(self): Visitor.__init__(self) self.system = system - self.types = None - self.temp = {} # Satisfy visitor issues. @@ -207,6 +205,8 @@ self.returns = [] self.return_locals = [] + self.types = None + self.temp = {} # Add namespace details to any structure involved. @@ -298,19 +298,21 @@ def visitLoadTemp(self, loadtemp): index = getattr(loadtemp, "index", None) - self.types = self.temp[index] + self.types = self.temp[index][-1] self.annotate(loadtemp) return loadtemp def visitStoreTemp(self, storetemp): storetemp.expr = self.dispatch(storetemp.expr) index = getattr(storetemp, "index", None) - self.temp[index] = self.types + if not self.temp.has_key(index): + self.temp[index] = [] + self.temp[index].append(self.types) return storetemp def visitReleaseTemp(self, releasetemp): index = getattr(releasetemp, "index", None) - del self.temp[index] + self.temp[index].pop() return releasetemp def visitLoadAttr(self, loadattr): @@ -343,12 +345,16 @@ self.namespace = Namespace() self.namespace.merge_namespace(saved_namespace) conditional.body = self.dispatches(conditional.body) + body_namespace = self.namespace self.namespace = Namespace() self.namespace.merge_namespace(saved_namespace) conditional.else_ = self.dispatches(conditional.else_) + else_namespace = self.namespace - self.namespace = saved_namespace + self.namespace = Namespace() + self.namespace.merge_namespace(body_namespace) + self.namespace.merge_namespace(else_namespace) return conditional def visitReturn(self, return_): @@ -388,13 +394,12 @@ types.append(default.types) invoke.args = args - invoke.types = expr # Now locate and invoke the subprogram. This can be complicated because # the target may be a class or object, and there may be many different # related subprograms. - invocations = [] + invocations = {} # Visit each callable in turn @@ -420,7 +425,10 @@ for subprogram in subprograms: if subprogram is not None: - invocations.append(self.invoke_subprogram(invoke, subprogram)) + self.invoke_subprogram(invoke, subprogram) + invocations[callable] = subprogram + + invoke.invocations = invocations return invoke diff -r b44426480e07 -r cc6abe59ef05 simplify.py --- a/simplify.py Thu Aug 03 01:00:40 2006 +0200 +++ b/simplify.py Thu Aug 03 01:02:47 2006 +0200 @@ -161,8 +161,7 @@ return result def _visitBuiltin(self, builtin, name): - result = Invoke(builtin, expr=LoadName(name=name)) - result.args = self.dispatches(builtin.nodes) + result = Invoke(builtin, expr=LoadName(name=name), args=self.dispatches(builtin.nodes), star=None, dstar=None) return result def visitTuple(self, tuple): @@ -172,7 +171,7 @@ return self._visitBuiltin(list, "list") def visitDict(self, dict): - result = Invoke(dict, expr=LoadName(name="dict")) + result = Invoke(dict, expr=LoadName(name="dict"), star=None, dstar=None) args = [] for key, value in dict.items: tuple = Invoke(expr=LoadName(name="tuple"), star=None, dstar=None) @@ -210,8 +209,7 @@ for compare, stmt in if_.tests: # Produce something like... # expr.__true__() ? body - test = Conditional(test=Invoke(expr=LoadAttr(expr=self.dispatch(compare), name="__true__"), - args=[], star=None, dstar=None)) + test = Conditional(test=Invoke(expr=LoadAttr(expr=self.dispatch(compare), name="__true__"), args=[], star=None, dstar=None)) test.body = self.dispatch(stmt) nodes.append(test) nodes = test.else_ = [] @@ -485,20 +483,19 @@ return result def visitNot(self, not_): - result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"), - args=[], star=None, dstar=None)) + result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"), args=[], star=None, dstar=None)) return result # Operators. def visitUnaryAdd(self, unaryadd): - return Invoke(unaryadd, expr=LoadAttr(expr=self.dispatch(unaryadd.expr), name="__pos__"), args=[]) + return Invoke(unaryadd, expr=LoadAttr(expr=self.dispatch(unaryadd.expr), name="__pos__"), args=[], star=None, dstar=None) def visitUnarySub(self, unarysub): - return Invoke(unarysub, expr=LoadAttr(expr=self.dispatch(unarysub.expr), name="__neg__"), args=[]) + return Invoke(unarysub, expr=LoadAttr(expr=self.dispatch(unarysub.expr), name="__neg__"), args=[], star=None, dstar=None) def visitInvert(self, invert): - return Invoke(invert, expr=LoadAttr(expr=self.dispatch(invert.expr), name="__invert__"), args=[]) + return Invoke(invert, expr=LoadAttr(expr=self.dispatch(invert.expr), name="__invert__"), args=[], star=None, dstar=None) def visitAdd(self, add): @@ -506,8 +503,8 @@ result = Choice(add) result.choices = [ - Invoke(expr=LoadAttr(expr=self.dispatch(add.left), name="__add__"), args=[self.dispatch(add.right)]), - Invoke(expr=LoadAttr(expr=self.dispatch(add.right), name="__radd__"), args=[self.dispatch(add.left)]) + Invoke(expr=LoadAttr(expr=self.dispatch(add.left), name="__add__"), args=[self.dispatch(add.right)], star=None, dstar=None), + Invoke(expr=LoadAttr(expr=self.dispatch(add.right), name="__radd__"), args=[self.dispatch(add.left)], star=None, dstar=None) ] return result @@ -529,7 +526,7 @@ name = augassign.node node = self.dispatch(name) get_incremented = StoreTemp( - expr=Invoke(expr=LoadAttr(expr=node, name=self.augassign_methods[augassign.op]), args=[expr]) + expr=Invoke(expr=LoadAttr(expr=node, name=self.augassign_methods[augassign.op]), args=[expr], star=None, dstar=None) ) store = StoreName(expr=LoadTemp(), name=name.name) result.code = [get_incremented, store, ReleaseTemp()] @@ -544,7 +541,7 @@ store_expr = StoreTemp(index="expr", expr=self.dispatch(getattr.expr)) node_attr = LoadAttr(expr=LoadTemp(index="expr"), name=getattr.attrname) get_incremented = StoreTemp( - expr=Invoke(expr=LoadAttr(expr=node_attr, name=self.augassign_methods[augassign.op]), args=[expr]) + expr=Invoke(expr=LoadAttr(expr=node_attr, name=self.augassign_methods[augassign.op]), args=[expr], star=None, dstar=None) ) store = StoreAttr(expr=LoadTemp(), lvalue=LoadTemp(index="expr"), name=getattr.attrname) result.code = [store_expr, get_incremented, store, ReleaseTemp(index="expr"), ReleaseTemp()] @@ -561,7 +558,7 @@ store_upper = StoreTemp(index="upper", expr=self.dispatch_or_none(slice.upper)) node_slice = self._visitSlice(slice, LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_APPLY") get_incremented = StoreTemp( - expr=Invoke(expr=LoadAttr(expr=node_slice, name=self.augassign_methods[augassign.op]), args=[expr]) + expr=Invoke(expr=LoadAttr(expr=node_slice, name=self.augassign_methods[augassign.op]), args=[expr], star=None, dstar=None) ) store = self._visitSlice(slice, LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_ASSIGN", LoadTemp()) result.code = [store_expr, store_lower, store_upper, get_incremented, store, @@ -579,7 +576,7 @@ store_subs = StoreTemp(index="subs", expr=subs) node_subscript = self._visitSubscript(subscript, LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_APPLY") get_incremented = StoreTemp( - expr=Invoke(expr=LoadAttr(expr=node_subscript, name=self.augassign_methods[augassign.op]), args=[expr]) + expr=Invoke(expr=LoadAttr(expr=node_subscript, name=self.augassign_methods[augassign.op]), args=[expr], star=None, dstar=None) ) store = self._visitSubscript(subscript, LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_ASSIGN", LoadTemp()) result.code = [store_expr, store_subs, get_incremented, store, ReleaseTemp(index="expr"), ReleaseTemp(index="subs"), ReleaseTemp()] @@ -600,9 +597,9 @@ if not in_sequence: expr = LoadTemp() else: - expr = Invoke(expr=LoadAttr(expr=LoadTemp(), name="next")) + expr = Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[]) result = Assign(asslist) - store = StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=expr))) + store = StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=expr), star=None, dstar=None, args=[])) release = ReleaseTemp() result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] return result @@ -613,7 +610,7 @@ if not in_sequence: return LoadTemp() else: - return Invoke(expr=LoadAttr(expr=LoadTemp(), name="next")) + return Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[]) def visitAssName(self, assname, in_sequence=0): expr = self._visitAssNameOrAttr(assname, in_sequence) @@ -629,13 +626,13 @@ def _visitSlice(self, slice, expr, lower, upper, flags, value=None): if flags == "OP_ASSIGN": args = [value] - result = Invoke(expr=LoadAttr(expr=expr, name="__setslice__")) + result = Invoke(expr=LoadAttr(expr=expr, name="__setslice__"), star=None, dstar=None, args=[]) elif flags == "OP_APPLY": args = [] - result = Invoke(expr=LoadAttr(expr=expr, name="__getslice__")) + result = Invoke(expr=LoadAttr(expr=expr, name="__getslice__"), star=None, dstar=None, args=[]) elif flags == "OP_DELETE": args = [] - result = Invoke(expr=LoadAttr(expr=expr, name="__delslice__")) + result = Invoke(expr=LoadAttr(expr=expr, name="__delslice__"), star=None, dstar=None, args=[]) else: raise NotImplementedError, flags @@ -656,13 +653,13 @@ def _visitSubscript(self, subscript, expr, subs, flags, value=None): if flags == "OP_ASSIGN": args = [value] - result = Invoke(expr=LoadAttr(expr=expr, name="__setitem__")) + result = Invoke(expr=LoadAttr(expr=expr, name="__setitem__"), star=None, dstar=None, args=[]) elif flags == "OP_APPLY": args = [] - result = Invoke(expr=LoadAttr(expr=expr, name="__getitem__")) + result = Invoke(expr=LoadAttr(expr=expr, name="__getitem__"), star=None, dstar=None, args=[]) elif flags == "OP_DELETE": args = [] - result = Invoke(expr=LoadAttr(expr=expr, name="__delitem__")) + result = Invoke(expr=LoadAttr(expr=expr, name="__delitem__"), star=None, dstar=None, args=[]) else: raise NotImplementedError, flags @@ -678,7 +675,7 @@ if len(subs) == 1: return self.dispatch(subs[0]) else: - return Invoke(expr=LoadName(name="tuple"), args=self.dispatches(subs)) + return Invoke(expr=LoadName(name="tuple"), args=self.dispatches(subs), star=None, dstar=None) def visitSubscript(self, subscript, in_sequence=0): value = self._visitAssNameOrAttr(subscript, in_sequence) @@ -811,8 +808,7 @@ # Include a conditional statement in the subprogram. test = Conditional(else_=[]) - test.test = Invoke(expr=LoadAttr(expr=self.dispatch(while_.test), name="__true__"), - args=[], star=None, dstar=None) + test.test = Invoke(expr=LoadAttr(expr=self.dispatch(while_.test), name="__true__"), args=[], star=None, dstar=None) # Inside the conditional, add a recursive invocation to the subprogram # if the test condition was satisfied. @@ -876,17 +872,14 @@ try_except = Try(body=[], else_=[], finally_=[]) test = Conditional( - test=Invoke( - expr=LoadName(name="isinstance"), - args=[LoadExc(), LoadName(name="StopIteration")], star=None, dstar=None - ), + test=Invoke(expr=LoadName(name="isinstance"), args=[LoadExc(), LoadName(name="StopIteration")], star=None, dstar=None), body=else_stmt, else_=[Raise(expr=LoadExc())]) try_except.handler = [test] assign = Assign() assign.code = [ - StoreTemp(expr=Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"))), + StoreTemp(expr=Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"), args=[], star=None, dstar=None)), self.dispatch(for_.assign), ReleaseTemp() ] @@ -909,7 +902,7 @@ result = Assign(for_) result.code = [ - StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)))), + StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)), args=[], star=None, dstar=None)), Invoke(expr=LoadRef(ref=subprogram), same_frame=1, produces_result=0, star=None, dstar=None, args=[]), ReleaseTemp() ]