[pypy-svn] r25425 - in pypy/dist/pypy/translator/cli: . test

antocuni at codespeak.net antocuni at codespeak.net
Wed Apr 5 23:39:06 CEST 2006


Author: antocuni
Date: Wed Apr  5 23:38:52 2006
New Revision: 25425

Modified:
   pypy/dist/pypy/translator/cli/class_.py
   pypy/dist/pypy/translator/cli/gencli.py
   pypy/dist/pypy/translator/cli/opcodes.py
   pypy/dist/pypy/translator/cli/test/compile.py
   pypy/dist/pypy/translator/cli/test/test_oo.py
Log:
Added support for inheritance


Modified: pypy/dist/pypy/translator/cli/class_.py
==============================================================================
--- pypy/dist/pypy/translator/cli/class_.py	(original)
+++ pypy/dist/pypy/translator/cli/class_.py	Wed Apr  5 23:38:52 2006
@@ -10,12 +10,19 @@
     def get_name(self):
         return self.name
 
+    def get_base_class(self):
+        base_class = self.classdef._superclass
+        if base_class is None or base_class._name == 'Object':
+            return '[mscorlib]System.Object'
+        else:
+            return base_class._name
+
     def render(self, ilasm):
         self.ilasm = ilasm
         if self.namespace:
             ilasm.begin_namespace(self.namespace)
 
-        ilasm.begin_class(self.name) # TODO: handle base class
+        ilasm.begin_class(self.name, self.get_base_class())
         for f_name, (f_type, f_default) in self.classdef._fields.iteritems():
             # TODO: handle default values
             ilasm.field(f_name, cts.lltype_to_cts(f_type))
@@ -23,7 +30,7 @@
         self._ctor()
 
         for m_name, m_meth in self.classdef._methods.iteritems():
-            # TODO: handle static methods
+            # TODO: handle static, class and unbound methods
             # TODO: should __init__ be rendered as a constructor?
             f = Function(m_meth.graph, m_name, is_method = True)
             f.render(ilasm)
@@ -36,7 +43,7 @@
     def _ctor(self):
         self.ilasm.begin_function('.ctor', [], 'void', False, 'specialname', 'rtspecialname', 'instance')
         self.ilasm.opcode('ldarg.0')
-        self.ilasm.call('instance void object::.ctor()') # TODO: base class
+        self.ilasm.call('instance void %s::.ctor()' % self.get_base_class())
         self.ilasm.opcode('ret')
         self.ilasm.end_function()
 

Modified: pypy/dist/pypy/translator/cli/gencli.py
==============================================================================
--- pypy/dist/pypy/translator/cli/gencli.py	(original)
+++ pypy/dist/pypy/translator/cli/gencli.py	Wed Apr  5 23:38:52 2006
@@ -47,6 +47,7 @@
 
         self.ilasm = IlasmGenerator(out, self.assembly_name)
         self.gen_all_functions()
+        self.find_superclasses()
         self.gen_classes()
         out.close()
         return self.tmpfile.strpath
@@ -67,6 +68,18 @@
                 f.render(self.ilasm)
                 self.classdefs.update(f.classdefs)
 
+    def find_superclasses(self):
+        classdefs = set()
+        pendings = self.classdefs
+
+        while pendings:
+            classdef = pendings.pop()
+            if classdef not in classdefs and classdef is not None:
+                classdefs.add(classdef)
+                pendings.add(classdef._superclass)
+
+        self.classdefs = classdefs
+
     def gen_classes(self):
         for classdef in self.classdefs:
             c = Class(classdef)

Modified: pypy/dist/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/cli/opcodes.py	(original)
+++ pypy/dist/pypy/translator/cli/opcodes.py	Wed Apr  5 23:38:52 2006
@@ -15,6 +15,8 @@
     'oosetfield':               [SetField],
     'oogetfield':               [GetField],
     'oosend':                   [CallMethod],
+    'ooupcast':                 DoNothing,
+    'oodowncast':               DoNothing, # TODO: is it really safe?
 
     
     'same_as':                  DoNothing, # TODO: does same_as really do nothing else than renaming?    

Modified: pypy/dist/pypy/translator/cli/test/compile.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/compile.py	(original)
+++ pypy/dist/pypy/translator/cli/test/compile.py	Wed Apr  5 23:38:52 2006
@@ -22,16 +22,16 @@
     else:
         print 'OK'
 
-class A:
+class Base:
     def __init__(self, x):
         self.x = x
 
-    def foo(self, a, b):
-        return self.x
+class Derived(Base):
+    pass
 
 def bar(x, y):
-    a = A(x)
-    return a.foo(x, y)
+    a = Derived(42)
+
 
 f = compile_function(bar, [int, int])
 

Modified: pypy/dist/pypy/translator/cli/test/test_oo.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_oo.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_oo.py	Wed Apr  5 23:38:52 2006
@@ -1,5 +1,13 @@
 from pypy.translator.cli.test.runtest import check
 
+def test_oo():
+    for name, func in globals().iteritems():
+        if not name.startswith('oo_'):
+            continue
+
+        yield check, func, [int, int], (42, 13)
+
+
 class MyClass:
     def __init__(self, x, y):
         self.x = x
@@ -11,6 +19,14 @@
     def compute_and_multiply(self, factor):
         return self.compute() * factor
 
+class MyDerivedClass(MyClass):
+    def __init__(self, x, y):
+        self.x = x
+        self.y = y
+
+    def compute(self):
+        return self.x - self.y
+
 def oo_compute(x, y):
     obj = MyClass(x, y)
     return obj.compute()
@@ -19,6 +35,14 @@
     obj = MyClass(x, y)
     return obj.compute_and_multiply(2)
 
-def test_oo():
-    yield check, oo_compute, [int, int], (42, 13)
-    yield check, oo_compute_multiply, [int, int], (42, 13)
+def oo_inheritance(x, y):
+    obj = MyDerivedClass(x, y)
+    return obj.compute_and_multiply(2)
+
+def helper(obj):
+    return obj.compute()
+
+def oo_liskov(x, y):
+    base = MyClass(x, y)
+    derived = MyDerivedClass(x, y)
+    return helper(base) + helper(derived)



More information about the Pypy-commit mailing list