Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: var-in-Some
Changeset: r73818:d41673491dc8
Date: 2014-10-05 23:33 +0100
http://bitbucket.org/pypy/pypy/changeset/d41673491dc8/

Log:    kill AnnotatedValue; store the annotation directly on the Variable

diff --git a/rpython/annotator/annrpython.py b/rpython/annotator/annrpython.py
--- a/rpython/annotator/annrpython.py
+++ b/rpython/annotator/annrpython.py
@@ -11,7 +11,6 @@
 from rpython.translator import simplify, transform
 from rpython.annotator import model as annmodel, signature
 from rpython.annotator.argument import simple_args
-from rpython.annotator.value import AnnotatedValue
 from rpython.annotator.bookkeeper import Bookkeeper
 
 import py
@@ -150,9 +149,9 @@
         if isinstance(variable, Constant):
             return type(variable.value)
         elif isinstance(variable, Variable):
-            cell = variable.binding
-            if cell:
-                return cell.ann.knowntype
+            s_variable = variable.annotation
+            if s_variable:
+                return s_variable.knowntype
             else:
                 return object
         else:
@@ -220,7 +219,7 @@
             raise annmodel.AnnotatorError(text)
         for graph in newgraphs:
             v = graph.getreturnvar()
-            if v.binding is None:
+            if v.annotation is None:
                 self.setbinding(v, annmodel.s_ImpossibleValue)
         # policy-dependent computation
         self.bookkeeper.compute_at_fixpoint()
@@ -228,10 +227,7 @@
     def annotation(self, arg):
         "Gives the SomeValue corresponding to the given Variable or Constant."
         if isinstance(arg, Variable):
-            annvalue = arg.binding
-            if annvalue is None:
-                return None
-            return annvalue.ann
+            return arg.annotation
         elif isinstance(arg, Constant):
             return self.bookkeeper.immutablevalue(arg.value)
         else:
@@ -244,27 +240,18 @@
             raise KeyError
         return s_arg
 
-    def annvalue(self, arg):
-        if isinstance(arg, Variable):
-            return arg.binding
-        else:
-            return AnnotatedValue(arg, 
self.bookkeeper.immutablevalue(arg.value))
-
     def typeannotation(self, t):
         return signature.annotation(t, self.bookkeeper)
 
     def setbinding(self, arg, s_value):
-        if arg.binding is None:
-            arg.binding = AnnotatedValue(arg, s_value)
-        else:
-            value = arg.binding
-            if value.ann is not None:
-                assert s_value.contains(value.ann)
-            value.ann = s_value
+        s_old = arg.annotation
+        if s_old is not None:
+            assert s_value.contains(s_old)
+        arg.annotation = s_value
 
     def transfer_binding(self, v_target, v_source):
-        assert v_source.binding is not None
-        v_target.binding = AnnotatedValue(v_target, v_source.binding.ann)
+        assert v_source.annotation is not None
+        v_target.annotation = v_source.annotation
 
     def warning(self, msg, pos=None):
         if pos is None:
@@ -583,18 +570,16 @@
     #___ creating the annotations based on operations ______
 
     def consider_op(self, op):
-        argcells = [self.annvalue(a) for a in op.args]
-
         # let's be careful about avoiding propagated SomeImpossibleValues
         # to enter an op; the latter can result in violations of the
         # more general results invariant: e.g. if SomeImpossibleValue enters 
is_
         #  is_(SomeImpossibleValue, None) -> SomeBool
         #  is_(SomeInstance(not None), None) -> SomeBool(const=False) ...
         # boom -- in the assert of setbinding()
-        for arg in argcells:
-            if isinstance(arg.ann, annmodel.SomeImpossibleValue):
+        for arg in op.args:
+            if isinstance(self.annotation(arg), annmodel.SomeImpossibleValue):
                 raise BlockedInference(self, op, -1)
-        resultcell = op.consider(self, *argcells)
+        resultcell = op.consider(self, *op.args)
         if resultcell is None:
             resultcell = annmodel.s_ImpossibleValue
         elif resultcell == annmodel.s_ImpossibleValue:
diff --git a/rpython/annotator/binaryop.py b/rpython/annotator/binaryop.py
--- a/rpython/annotator/binaryop.py
+++ b/rpython/annotator/binaryop.py
@@ -26,8 +26,8 @@
 @op.is_.register(SomeObject, SomeObject)
 def is__default(annotator, obj1, obj2):
     r = SomeBool()
-    s_obj1 = obj1.ann
-    s_obj2 = obj2.ann
+    s_obj1 = annotator.annotation(obj1)
+    s_obj2 = annotator.annotation(obj2)
     if s_obj2.is_constant():
         if s_obj1.is_constant():
             r.const = s_obj1.const is s_obj2.const
@@ -39,18 +39,17 @@
     knowntypedata = {}
 
     def bind(src_obj, tgt_obj):
-        if hasattr(tgt_obj.ann, 'is_type_of') and src_obj.ann.is_constant():
+        if hasattr(annotator.annotation(tgt_obj), 'is_type_of') and 
annotator.annotation(src_obj).is_constant():
             add_knowntypedata(
                 knowntypedata, True,
-                tgt_obj.ann.is_type_of,
-                getbookkeeper().valueoftype(src_obj.ann.const))
-        add_knowntypedata(knowntypedata, True, [tgt_obj.value], src_obj.ann)
-        s_nonnone = tgt_obj.ann
-        if (src_obj.ann.is_constant() and src_obj.ann.const is None and
-                tgt_obj.ann.can_be_none()):
-            s_nonnone = tgt_obj.ann.nonnoneify()
-        add_knowntypedata(knowntypedata,
-                            False, [tgt_obj.value], s_nonnone)
+                annotator.annotation(tgt_obj).is_type_of,
+                
getbookkeeper().valueoftype(annotator.annotation(src_obj).const))
+        add_knowntypedata(knowntypedata, True, [tgt_obj], 
annotator.annotation(src_obj))
+        s_nonnone = annotator.annotation(tgt_obj)
+        if (annotator.annotation(src_obj).is_constant() and 
annotator.annotation(src_obj).const is None and
+                annotator.annotation(tgt_obj).can_be_none()):
+            s_nonnone = annotator.annotation(tgt_obj).nonnoneify()
+        add_knowntypedata(knowntypedata, False, [tgt_obj], s_nonnone)
 
     bind(obj2, obj1)
     bind(obj1, obj2)
@@ -60,7 +59,7 @@
 def _make_cmp_annotator_default(cmp_op):
     @cmp_op.register(SomeObject, SomeObject)
     def default_annotate(annotator, obj1, obj2):
-        s_1, s_2 = obj1.ann, obj2.ann
+        s_1, s_2 = annotator.annotation(obj1), annotator.annotation(obj2)
         if s_1.is_immutable_constant() and s_2.is_immutable_constant():
             return immutablevalue(cmp_op.pyfunc(s_1.const, s_2.const))
         else:
@@ -247,7 +246,7 @@
     @cmp_op.register(SomeInteger, SomeInteger)
     def _compare_helper(annotator, int1, int2):
         r = SomeBool()
-        s_int1, s_int2 = int1.ann, int2.ann
+        s_int1, s_int2 = annotator.annotation(int1), annotator.annotation(int2)
         if s_int1.is_immutable_constant() and s_int2.is_immutable_constant():
             r.const = cmp_op.pyfunc(s_int1.const, s_int2.const)
         #
@@ -269,21 +268,21 @@
             if s_int0.knowntype is bool:
                 return int
             return s_int0.knowntype
-        if s_int1.nonneg and isinstance(int2.value, Variable):
+        if s_int1.nonneg and isinstance(int2, Variable):
             case = cmp_op.opname in ('lt', 'le', 'eq')
-            add_knowntypedata(knowntypedata, case, [int2.value],
+            add_knowntypedata(knowntypedata, case, [int2],
                               SomeInteger(nonneg=True, 
knowntype=tointtype(s_int2)))
-        if s_int2.nonneg and isinstance(int1.value, Variable):
+        if s_int2.nonneg and isinstance(int1, Variable):
             case = cmp_op.opname in ('gt', 'ge', 'eq')
-            add_knowntypedata(knowntypedata, case, [int1.value],
+            add_knowntypedata(knowntypedata, case, [int1],
                               SomeInteger(nonneg=True, 
knowntype=tointtype(s_int1)))
         r.set_knowntypedata(knowntypedata)
         # a special case for 'x < 0' or 'x >= 0',
         # where 0 is a flow graph Constant
         # (in this case we are sure that it cannot become a r_uint later)
-        if (isinstance(int2.value, Constant) and
-                type(int2.value.value) is int and  # filter out Symbolics
-                int2.value.value == 0):
+        if (isinstance(int2, Constant) and
+                type(int2.value) is int and  # filter out Symbolics
+                int2.value == 0):
             if s_int1.nonneg:
                 if cmp_op.opname == 'lt':
                     r.const = False
@@ -718,9 +717,9 @@
 def is__PBC_PBC(annotator, pbc1, pbc2):
     s = is__default(annotator, pbc1, pbc2)
     if not s.is_constant():
-        if not pbc1.ann.can_be_None or not pbc2.ann.can_be_None:
-            for desc in pbc1.ann.descriptions:
-                if desc in pbc2.ann.descriptions:
+        if not annotator.annotation(pbc1).can_be_None or not 
annotator.annotation(pbc2).can_be_None:
+            for desc in annotator.annotation(pbc1).descriptions:
+                if desc in annotator.annotation(pbc2).descriptions:
                     break
             else:
                 s.const = False    # no common desc in the two sets
diff --git a/rpython/annotator/test/test_annrpython.py 
b/rpython/annotator/test/test_annrpython.py
--- a/rpython/annotator/test/test_annrpython.py
+++ b/rpython/annotator/test/test_annrpython.py
@@ -861,7 +861,7 @@
         all_vars = set().union(*[block.getvariables() for block in 
graph.iterblocks()])
         print all_vars
         for var in all_vars:
-            s_value = var.binding.ann
+            s_value = var.annotation
             if isinstance(s_value, annmodel.SomeList):
                 assert not s_value.listdef.listitem.resized
                 assert not s_value.listdef.listitem.mutated
diff --git a/rpython/annotator/unaryop.py b/rpython/annotator/unaryop.py
--- a/rpython/annotator/unaryop.py
+++ b/rpython/annotator/unaryop.py
@@ -24,18 +24,18 @@
 @op.type.register(SomeObject)
 def type_SomeObject(annotator, arg):
     r = SomeType()
-    r.is_type_of = [arg.value]
+    r.is_type_of = [arg]
     return r
 
 @op.bool.register(SomeObject)
 def bool_SomeObject(annotator, obj):
     r = SomeBool()
-    obj.ann.bool_behavior(r)
-    s_nonnone_obj = obj.ann
+    annotator.annotation(obj).bool_behavior(r)
+    s_nonnone_obj = annotator.annotation(obj)
     if s_nonnone_obj.can_be_none():
         s_nonnone_obj = s_nonnone_obj.nonnoneify()
     knowntypedata = {}
-    add_knowntypedata(knowntypedata, True, [obj.value], s_nonnone_obj)
+    add_knowntypedata(knowntypedata, True, [obj], s_nonnone_obj)
     r.set_knowntypedata(knowntypedata)
     return r
 
@@ -46,11 +46,11 @@
 
 @op.simple_call.register(SomeObject)
 def simple_call_SomeObject(annotator, func, *args):
-    return func.ann.call(simple_args([arg.ann for arg in args]))
+    return 
annotator.annotation(func).call(simple_args([annotator.annotation(arg) for arg 
in args]))
 
 @op.call_args.register(SomeObject)
 def call_args(annotator, func, *args):
-    return func.ann.call(complex_args([arg.ann for arg in args]))
+    return 
annotator.annotation(func).call(complex_args([annotator.annotation(arg) for arg 
in args]))
 
 class __extend__(SomeObject):
 
@@ -248,7 +248,7 @@
 
 @op.contains.register(SomeList)
 def contains_SomeList(annotator, obj, element):
-    obj.ann.listdef.generalize(element.ann)
+    annotator.annotation(obj).listdef.generalize(annotator.annotation(element))
     return s_Bool
 contains_SomeList.can_only_throw = []
 
@@ -345,8 +345,8 @@
 
 @op.contains.register(SomeDict)
 def contains_SomeDict(annotator, dct, element):
-    dct.ann.dictdef.generalize_key(element.ann)
-    if dct.ann._is_empty():
+    
annotator.annotation(dct).dictdef.generalize_key(annotator.annotation(element))
+    if annotator.annotation(dct)._is_empty():
         s_bool = SomeBool()
         s_bool.const = False
         return s_bool
@@ -437,11 +437,11 @@
 @op.contains.register(SomeString)
 @op.contains.register(SomeUnicodeString)
 def contains_String(annotator, string, char):
-    if char.ann.is_constant() and char.ann.const == "\0":
+    if annotator.annotation(char).is_constant() and 
annotator.annotation(char).const == "\0":
         r = SomeBool()
         knowntypedata = {}
-        add_knowntypedata(knowntypedata, False, [string.value],
-                          string.ann.nonnulify())
+        add_knowntypedata(knowntypedata, False, [string],
+                          annotator.annotation(string).nonnulify())
         r.set_knowntypedata(knowntypedata)
         return r
     else:
diff --git a/rpython/annotator/value.py b/rpython/annotator/value.py
deleted file mode 100644
--- a/rpython/annotator/value.py
+++ /dev/null
@@ -1,9 +0,0 @@
-""" AnnotatedValue """
-
-class AnnotatedValue(object):
-    def __init__(self, value, annotation):
-        self.value = value
-        self.ann = annotation
-
-    def __repr__(self):
-        return "AnnotatedValue(%s, %r)" % (self.value, self.ann)
diff --git a/rpython/flowspace/model.py b/rpython/flowspace/model.py
--- a/rpython/flowspace/model.py
+++ b/rpython/flowspace/model.py
@@ -250,7 +250,7 @@
 
 
 class Variable(object):
-    __slots__ = ["_name", "_nr", "binding", "concretetype"]
+    __slots__ = ["_name", "_nr", "annotation", "concretetype"]
 
     dummyname = 'v'
     namesdict = {dummyname: (dummyname, 0)}
@@ -273,7 +273,7 @@
     def __init__(self, name=None):
         self._name = self.dummyname
         self._nr = -1
-        self.binding = None
+        self.annotation = None
         # numbers are bound lazily, when the name is requested
         if name is not None:
             self.rename(name)
diff --git a/rpython/flowspace/operation.py b/rpython/flowspace/operation.py
--- a/rpython/flowspace/operation.py
+++ b/rpython/flowspace/operation.py
@@ -97,7 +97,7 @@
         return None
 
     def consider(self, annotator, *args):
-        args_s = [arg.ann for arg in args]
+        args_s = [annotator.annotation(arg) for arg in args]
         spec = type(self).get_specialization(*args_s)
         return spec(annotator, *args)
 
@@ -166,7 +166,7 @@
         raise AnnotatorError("Unknown operation")
 
     def get_can_only_throw(self, annotator):
-        args_s = [annotator.binding(v) for v in self.args]
+        args_s = [annotator.annotation(v) for v in self.args]
         spec = type(self).get_specialization(*args_s)
         return read_can_only_throw(spec, args_s[0])
 
@@ -176,7 +176,7 @@
             impl = getattr(s_arg, cls.opname)
 
             def specialized(annotator, arg, *other_args):
-                return impl(*[x.ann for x in other_args])
+                return impl(*[annotator.annotation(x) for x in other_args])
             try:
                 specialized.can_only_throw = impl.can_only_throw
             except AttributeError:
@@ -202,7 +202,7 @@
             impl = getattr(pair(s_arg1, s_arg2), cls.opname)
 
             def specialized(annotator, arg1, arg2, *other_args):
-                return impl(*[x.ann for x in other_args])
+                return impl(*[annotator.annotation(x) for x in other_args])
             try:
                 specialized.can_only_throw = impl.can_only_throw
             except AttributeError:
@@ -212,7 +212,7 @@
             return cls._registry[type(s_arg1), type(s_arg2)]
 
     def get_can_only_throw(self, annotator):
-        args_s = [annotator.binding(v) for v in self.args]
+        args_s = [annotator.annotation(v) for v in self.args]
         spec = type(self).get_specialization(*args_s)
         return read_can_only_throw(spec, args_s[0], args_s[1])
 
@@ -457,7 +457,7 @@
     canraise = []
 
     def consider(self, annotator, *args):
-        return SomeTuple(items=[arg.ann for arg in args])
+        return SomeTuple(items=[annotator.annotation(arg) for arg in args])
 
 
 class NewList(HLOperation):
@@ -465,7 +465,7 @@
     canraise = []
 
     def consider(self, annotator, *args):
-        return annotator.bookkeeper.newlist(*[arg.ann for arg in args])
+        return annotator.bookkeeper.newlist(*[annotator.annotation(arg) for 
arg in args])
 
 
 class Pow(PureOperation):
diff --git a/rpython/rtyper/callparse.py b/rpython/rtyper/callparse.py
--- a/rpython/rtyper/callparse.py
+++ b/rpython/rtyper/callparse.py
@@ -19,7 +19,7 @@
 
 def getrresult(rtyper, graph):
     """Return the repr of the result variable of the 'graph'."""
-    if graph.getreturnvar().binding is not None:
+    if graph.getreturnvar().annotation is not None:
         return rtyper.bindingrepr(graph.getreturnvar())
     else:
         return lltype.Void
diff --git a/rpython/rtyper/rtyper.py b/rpython/rtyper/rtyper.py
--- a/rpython/rtyper/rtyper.py
+++ b/rpython/rtyper/rtyper.py
@@ -496,7 +496,7 @@
                     hop.r_result, op.opname, resulttype))
             # figure out if the resultvar is a completely fresh Variable or not
             if (isinstance(resultvar, Variable) and
-                resultvar.binding is None and
+                resultvar.annotation is None and
                 resultvar not in varmapping):
                 # fresh Variable: rename it to the previously existing 
op.result
                 varmapping[resultvar] = op.result
diff --git a/rpython/translator/transform.py b/rpython/translator/transform.py
--- a/rpython/translator/transform.py
+++ b/rpython/translator/transform.py
@@ -124,14 +124,14 @@
             elif op.opname == 'contains' and op.args[0] in newlist_sources:
                 items = {}
                 for v in newlist_sources[op.args[0]]:
-                    s = self.binding(v)
+                    s = self.annotation(v)
                     if not s.is_immutable_constant():
                         break
                     items[s.const] = None
                 else:
                     # all arguments of the newlist are annotation constants
                     op.args[0] = Constant(items)
-                    s_dict = self.binding(op.args[0])
+                    s_dict = self.annotation(op.args[0])
                     s_dict.dictdef.generalize_key(self.binding(op.args[1]))
 
 
@@ -168,9 +168,9 @@
     "Fix a block whose end can never be reached at run-time."
     # search the operation that cannot succeed
     can_succeed    = [op for op in block.operations
-                         if op.result.binding is not None]
+                         if op.result.annotation is not None]
     cannot_succeed = [op for op in block.operations
-                         if op.result.binding is None]
+                         if op.result.annotation is None]
     n = len(can_succeed)
     # check consistency
     assert can_succeed == block.operations[:n]
diff --git a/rpython/translator/unsimplify.py b/rpython/translator/unsimplify.py
--- a/rpython/translator/unsimplify.py
+++ b/rpython/translator/unsimplify.py
@@ -6,8 +6,7 @@
     """Make a copy of the Variable v, preserving annotations and 
concretetype."""
     assert isinstance(v, Variable)
     newvar = Variable(v)
-    if annotator is not None and v.binding is not None:
-        annotator.transfer_binding(newvar, v)
+    newvar.annotation = v.annotation
     if hasattr(v, 'concretetype'):
         newvar.concretetype = v.concretetype
     return newvar
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to