Commits

Antonio Cuni committed 1267691

add a new ootype operations 'getsuperclassof', the get the superclass of
objects of type ootype.Class

Comments (0)

Files changed (9)

pypy/annotation/builtin.py

     assert isinstance(class2, SomeOOClass) 
     return s_Bool
 
+def getsuperclassof(class_):
+    assert isinstance(class_, SomeOOClass)
+    return SomeOOClass(class_.ootype._superclass)
+
 def runtimenew(c):
     assert isinstance(c, SomeOOClass)
     if c.ootype is None:
 BUILTIN_ANALYZERS[ootype.runtimenew] = runtimenew
 BUILTIN_ANALYZERS[ootype.classof] = classof
 BUILTIN_ANALYZERS[ootype.subclassof] = subclassof
+BUILTIN_ANALYZERS[ootype.getsuperclassof] = getsuperclassof
 BUILTIN_ANALYZERS[ootype.ooupcast] = ooupcast
 BUILTIN_ANALYZERS[ootype.oodowncast] = oodowncast
 BUILTIN_ANALYZERS[ootype.cast_to_object] = cast_to_object

pypy/rpython/lltypesystem/lloperation.py

     'instanceof':           LLOp(oo=True, canfold=True),
     'classof':              LLOp(oo=True, canfold=True),
     'subclassof':           LLOp(oo=True, canfold=True),
+    'getsuperclassof':      LLOp(oo=True, canfold=True),
     'oostring':             LLOp(oo=True, sideeffects=False),
     'ooparse_int':          LLOp(oo=True, canraise=(ValueError,)),
     'ooparse_float':        LLOp(oo=True, canraise=(ValueError,)),

pypy/rpython/ootypesystem/ooopimpl.py

 def op_subclassof(class1, class2):
     return ootype.subclassof(class1, class2)
 
+def op_getsuperclassof(class_):
+    return ootype.getsuperclassof(class_)
+
 def op_oogetfield(inst, name):
     checkinst(inst)
     if not ootype.typeOf(inst)._hints.get('immutable'):

pypy/rpython/ootypesystem/ootype.py

     def _cast_to_object(self):
         return make_object(self)
 
+    def _get_superclass(self):
+        SUPER = self._INSTANCE._superclass
+        return runtimeClass(SUPER)
+
     def __repr__(self):
         return '%s(%s)' % (self.__class__.__name__, self._INSTANCE)
 
     assert class2 is not nullruntimeclass
     return isSubclass(class1._INSTANCE, class2._INSTANCE)
 
+def getsuperclassof(class_):
+    return class_._get_superclass()
+
 def addFields(INSTANCE, fields, with_default=False):
     INSTANCE._add_fields(fields, with_default)
 

pypy/rpython/ootypesystem/rbuiltin.py

     return hop.genop('subclassof', vlist,
                      resulttype = ootype.Bool)
 
+def rtype_getsuperclassof(hop):
+    assert isinstance(hop.args_s[0], annmodel.SomeOOClass)
+    vlist = hop.inputargs(hop.args_r[0])
+    return hop.genop('getsuperclassof', vlist,
+                     resulttype = ootype.Class)
+
 def rtype_instanceof(hop):
     INSTANCE = hop.args_v[1].value
     v_inst = hop.inputarg(hop.args_r[0], arg=0)
 BUILTIN_TYPER[ootype.null] = rtype_null
 BUILTIN_TYPER[ootype.classof] = rtype_classof
 BUILTIN_TYPER[ootype.subclassof] = rtype_subclassof
+BUILTIN_TYPER[ootype.getsuperclassof] = rtype_getsuperclassof
 BUILTIN_TYPER[ootype.instanceof] = rtype_instanceof
 BUILTIN_TYPER[ootype.runtimenew] = rtype_runtimenew
 BUILTIN_TYPER[ootype.ooupcast] = rtype_ooupcast

pypy/rpython/ootypesystem/test/test_ootype.py

         1, 1, 1,
         ]
 
+def test_getsuperclassof():
+    A = Instance("A", ROOT)
+    B = Instance("B", A)
+    C = Instance("C", B)
+    clsA = runtimeClass(A)
+    clsB = runtimeClass(B)
+    clsC = runtimeClass(C)
+    assert getsuperclassof(clsC) is clsB
+    assert getsuperclassof(clsB) is clsA
+    assert getsuperclassof(clsA) is runtimeClass(ROOT)
+
 def test_static_method_equality():
     SM = StaticMethod([], Signed)
     SM1 = StaticMethod([], Signed)

pypy/rpython/test/test_rclass.py

                 return ootype.NULL
         res = self.interpret(fn_mix_null, [False])
         assert res is ootype.NULL
+
+    def test_getsuperclassof(self):
+        A = ootype.Instance("A", ootype.ROOT)
+        B = ootype.Instance("B", A)
+        clsA = ootype.runtimeClass(A)
+        clsB = ootype.runtimeClass(B)
+
+        def fn(flag):
+            cls = flag and clsA or clsB
+            return ootype.getsuperclassof(cls) is clsA
+
+        res = self.interpret(fn, [False], backendopt=False)
+        assert res

pypy/translator/cli/opcodes.py

     'classof':                  [PushAllArgs, 'callvirt instance class [mscorlib]System.Type object::GetType()'],
     'instanceof':               [CastTo, 'ldnull', 'cgt.un'],
     'subclassof':               [PushAllArgs, 'call bool [pypylib]pypy.runtime.Utils::SubclassOf(class [mscorlib]System.Type, class[mscorlib]System.Type)'],
+    'getsuperclassof':          [PushAllArgs, 'callvirt instance class [mscorlib]System.Type class [mscorlib]System.Type::get_BaseType()'],
     'gc_id':                    [PushAllArgs, 'call int32 [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetHashCode(object)'],   # XXX not implemented
     'gc_identityhash':          [PushAllArgs, 'call int32 [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetHashCode(object)'],
     'oostring':                 [OOString],

pypy/translator/jvm/test/test_class.py

     def test_specialize_methods(self):
         py.test.skip('ABSTRACT METHOD FIX: RE-TEST AFTER MERGE')
 
+    def test_getsuperclassof(self):
+        py.test.skip('fixme')
+
 class TestJvmSpecialCase(JvmTest, BaseTestSpecialcase):
     pass