[pypy-svn] r18479 - in pypy/branch/hl-backend/pypy: annotation rpython/ootype rpython/ootype/test

ac at codespeak.net ac at codespeak.net
Wed Oct 12 16:27:53 CEST 2005


Author: ac
Date: Wed Oct 12 16:27:53 2005
New Revision: 18479

Modified:
   pypy/branch/hl-backend/pypy/annotation/bookkeeper.py
   pypy/branch/hl-backend/pypy/annotation/builtin.py
   pypy/branch/hl-backend/pypy/annotation/model.py
   pypy/branch/hl-backend/pypy/annotation/unaryop.py
   pypy/branch/hl-backend/pypy/rpython/ootype/ootype.py
   pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ooann.py
Log:
(bert, arre)
Teach the annotator about the null function, methods and static methods.



Modified: pypy/branch/hl-backend/pypy/annotation/bookkeeper.py
==============================================================================
--- pypy/branch/hl-backend/pypy/annotation/bookkeeper.py	(original)
+++ pypy/branch/hl-backend/pypy/annotation/bookkeeper.py	Wed Oct 12 16:27:53 2005
@@ -353,6 +353,8 @@
         elif isinstance(x, lladdress.address):
             assert x is lladdress.NULL
             result= SomeAddress(is_null=True)
+        elif isinstance(x, ootype._static_meth):
+            result = SomeStaticMeth(ootype.typeOf(x))
         elif callable(x) or isinstance(x, staticmethod): # XXX
             # maybe 'x' is a method bound to a not-yet-frozen cache?
             # fun fun fun.

Modified: pypy/branch/hl-backend/pypy/annotation/builtin.py
==============================================================================
--- pypy/branch/hl-backend/pypy/annotation/builtin.py	(original)
+++ pypy/branch/hl-backend/pypy/annotation/builtin.py	Wed Oct 12 16:27:53 2005
@@ -374,6 +374,12 @@
     r = SomeRef(ootype.typeOf(i))
     return r
 
+def null(C):
+    assert C.is_constant()
+    i = ootype.null(C.const)
+    r = SomeRef(ootype.typeOf(i))
+    return r
+
 def instanceof(c, C):
     assert C.is_constant()
     assert isinstance(C.const, ootype.Class)
@@ -381,6 +387,7 @@
 
 BUILTIN_ANALYZERS[ootype.instanceof] = instanceof
 BUILTIN_ANALYZERS[ootype.new] = new
+BUILTIN_ANALYZERS[ootype.null] = null
 
 #________________________________
 # non-gc objects

Modified: pypy/branch/hl-backend/pypy/annotation/model.py
==============================================================================
--- pypy/branch/hl-backend/pypy/annotation/model.py	(original)
+++ pypy/branch/hl-backend/pypy/annotation/model.py	Wed Oct 12 16:27:53 2005
@@ -435,7 +435,15 @@
     def __init__(self, ootype):
         self.ootype = ootype
 
+class SomeBoundMeth(SomeObject):
+    def __init__(self, ootype, name):
+        self.ootype = ootype
+        self.name = name
 
+class SomeStaticMeth(SomeObject):
+    def __init__(self, method):
+        self.method = method
+        
 from pypy.rpython import lltype
 from pypy.rpython.ootype import ootype
 

Modified: pypy/branch/hl-backend/pypy/annotation/unaryop.py
==============================================================================
--- pypy/branch/hl-backend/pypy/annotation/unaryop.py	(original)
+++ pypy/branch/hl-backend/pypy/annotation/unaryop.py	Wed Oct 12 16:27:53 2005
@@ -566,7 +566,9 @@
 
 
 # annotation of low-level types
-from pypy.annotation.model import SomePtr, SomeRef, ll_to_annotation, annotation_to_lltype
+from pypy.annotation.model import SomePtr, SomeRef, SomeBoundMeth, SomeStaticMeth
+from pypy.annotation.model import ll_to_annotation, annotation_to_lltype
+
 class __extend__(SomePtr):
 
     def getattr(p, s_attr):
@@ -592,12 +594,28 @@
     def is_true(p):
         return SomeBool()
 
+from pypy.rpython.ootype import ootype
 class __extend__(SomeRef):
-    def getattr(p, s_attr):
-        assert s_attr.is_constant(), "getattr on ref %r with non-constant field-name" % p.ootype
-        v = getattr(p.ootype._example(), s_attr.const)
+    def getattr(r, s_attr):
+        assert s_attr.is_constant(), "getattr on ref %r with non-constant field-name" % r.ootype
+        v = getattr(r.ootype._example(), s_attr.const)
+        if isinstance(v, ootype._bound_meth):
+            return SomeBoundMeth(r.ootype, s_attr.const)
+        return ll_to_annotation(v)
+
+class __extend__(SomeBoundMeth):
+    def simple_call(m, *args_s):
+        llargs = [annotation_to_lltype(arg_s)._example() for arg_s in args_s]
+        inst = m.ootype._example()
+        v = getattr(inst, m.name)(*llargs)
         return ll_to_annotation(v)
 
+class __extend__(SomeStaticMeth):
+    def simple_call(m, *args_s):
+        llargs = [annotation_to_lltype(arg_s)._example() for arg_s in args_s]
+        smeth = m.method._example()
+        v = smeth(*llargs)
+        return ll_to_annotation(v)
 
 #_________________________________________
 # memory addresses
@@ -611,3 +629,4 @@
         assert s_attr.const in lladdress.supported_access_types
         return SomeTypedAddressAccess(
             lladdress.supported_access_types[s_attr.const])
+

Modified: pypy/branch/hl-backend/pypy/rpython/ootype/ootype.py
==============================================================================
--- pypy/branch/hl-backend/pypy/rpython/ootype/ootype.py	(original)
+++ pypy/branch/hl-backend/pypy/rpython/ootype/ootype.py	Wed Oct 12 16:27:53 2005
@@ -100,6 +100,10 @@
     	self.ARGS = tuple(args)
 	self.RESULT = result
 
+    def _example(self):
+        _retval = self.RESULT._example()
+        return _static_meth(self, _callable=lambda *args: _retval)
+    
 class Meth(StaticMethod):
 
     def __init__(self, args, result):
@@ -190,11 +194,14 @@
         return _bound_meth(inst, self)
 
 class _bound_meth(object):
-
     def __init__(self, inst, meth):
+        #self._TYPE = self
         self.inst = inst
         self.meth = meth
 
+    #def _example(self):
+    #    return self
+    
     def __call__(self, *args):
         return self.meth._checkargs(args)(self.inst, *args)
 

Modified: pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ooann.py
==============================================================================
--- pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ooann.py	(original)
+++ pypy/branch/hl-backend/pypy/rpython/ootype/test/test_ooann.py	Wed Oct 12 16:27:53 2005
@@ -8,10 +8,10 @@
     C = Class("test", None, {'a': Signed})
     
     def oof():
-    	c = new(C)
-	return c.a
+        c = new(C)
+        return c.a
 
-    a =	RPythonAnnotator()
+    a = RPythonAnnotator()
     s = a.build_types(oof, [])
     #a.translator.view()
 
@@ -21,11 +21,78 @@
     C = Class("test", None, {'a': Signed})
     
     def oof():
-    	c = new(C)
-	return instanceof(c, C)
+        c = new(C)
+        return instanceof(c, C)
 
-    a =	RPythonAnnotator()
+    a = RPythonAnnotator()
     s = a.build_types(oof, [])
     #a.translator.view()
 
     assert s.knowntype == bool
+
+def test_simple_null():
+    C = Class("test", None, {'a': Signed})
+    
+    def oof():
+        c = null(C)
+        return c
+
+    a = RPythonAnnotator()
+    s = a.build_types(oof, [])
+    #a.translator.view()
+
+    assert s == annmodel.SomeRef(C)
+
+def test_method():
+    C = Class("test", None, {"a": (Signed, 3)})
+
+    M = Meth([C], Signed)
+    def m_(self, other):
+       return self.a + other.a
+    m = meth(M, _name="m", _callable=m_)
+
+    addMethods(C, {"m": m})
+
+    def oof():
+        c = new(C)
+        return c.m(c)
+    
+    a = RPythonAnnotator()
+    s = a.build_types(oof, [])
+    # a.translator.view()
+
+    assert s.knowntype == int
+
+def test_unionof():
+    C1 = Class("C1", None)
+    C2 = Class("C2", C1)
+    C3 = Class("C3", C1)
+
+    def oof(f):
+        if f:
+            c = new(C2)
+        else:
+            c = new(C3)
+        return c
+
+    a = RPythonAnnotator()
+    s = a.build_types(oof, [bool])
+    #a.translator.view()
+
+    assert s == annmodel.SomeRef(C1)
+
+def test_static_method():
+    F = StaticMethod([Signed, Signed], Signed)
+    def f_(a, b):
+       return a+b
+    f = static_meth(F, "f", _callable=f_)
+
+    def oof():
+        return f(2,3)
+
+    a = RPythonAnnotator()
+    s = a.build_types(oof, [])
+    #a.translator.view()
+
+    assert s.knowntype = int
+



More information about the Pypy-commit mailing list