[pypy-svn] r33505 - in pypy/dist/pypy: rpython/ootypesystem translator/cli translator/cli/test

antocuni at codespeak.net antocuni at codespeak.net
Fri Oct 20 18:28:17 CEST 2006


Author: antocuni
Date: Fri Oct 20 18:28:16 2006
New Revision: 33505

Modified:
   pypy/dist/pypy/rpython/ootypesystem/ootype.py
   pypy/dist/pypy/translator/cli/dotnet.py
   pypy/dist/pypy/translator/cli/metavm.py
   pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
A bit of refactoring in ootype and gencli to make them share the same
code for handling both static and instance overloaded methods.



Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py	Fri Oct 20 18:28:16 2006
@@ -879,15 +879,7 @@
         return bound_meth(*args)
 
 
-class _overloaded_meth(_meth):
-    _bound_class = _overloaded_bound_meth
-    
-    def __init__(self, *overloadings, **attrs):
-        assert '_callable' not in attrs
-        _meth.__init__(self, Meth([], Void), _callable=None, **attrs) # use a fake method type
-        self._overloadings = overloadings
-        self._check_overloadings()
-
+class _overloaded_mixin(object):
     def _check_overloadings(self):
         signatures = set()
         for meth in self._overloadings:
@@ -906,6 +898,17 @@
                 return meth
         raise TypeError, 'No suitable overloading found for method'
 
+
+class _overloaded_meth(_meth, _overloaded_mixin):
+    _bound_class = _overloaded_bound_meth
+    _desc_class = _overloaded_meth_desc
+
+    def __init__(self, *overloadings, **attrs):
+        assert '_callable' not in attrs
+        _meth.__init__(self, Meth([], Void), _callable=None, **attrs) # use a fake method type
+        self._overloadings = overloadings
+        self._check_overloadings()
+
     def _get_desc(self, name, ARGS):
         meth = self._resolve_overloading(ARGS)
         return _overloaded_meth_desc(name, meth._TYPE)

Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Fri Oct 20 18:28:16 2006
@@ -1,6 +1,6 @@
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.ootypesystem import ootype
-from pypy.rpython.ootypesystem.ootype import meth, overload, Meth
+from pypy.rpython.ootypesystem.ootype import meth, overload, Meth, StaticMethod
 from pypy.annotation import model as annmodel
 from pypy.rpython.rmodel import Repr
 
@@ -28,15 +28,15 @@
         self.cli_class = cli_class
         self.meth_name = meth_name
 
+    def simple_call(self, *args_s):
+        return self.cli_class._ann_static_method(self.meth_name, args_s)
+
     def rtyper_makerepr(self, rtyper):
         return CliStaticMethodRepr(self.cli_class, self.meth_name)
 
     def rtyper_makekey(self):
         return self.__class__, self.cli_class, self.meth_name
 
-    def simple_call(self, *args_s):
-        return self.cli_class._ann_static_method(self.meth_name, args_s)
-
 
 
 ## Rtyper model
@@ -64,36 +64,59 @@
         self.cli_class = cli_class
         self.meth_name = meth_name
 
-    def _build_desc(self, args_v, resulttype):
-        argtypes = [v.concretetype for v in args_v]
-        return StaticMethodDesc(self.cli_class._INSTANCE._name, self.meth_name, argtypes, resulttype)
+    def _build_desc(self, args_v):
+        ARGS = tuple([v.concretetype for v in args_v])
+        return self.cli_class._lookup(self.meth_name, ARGS)
 
     def rtype_simple_call(self, hop):
         vlist = []
         for i, repr in enumerate(hop.args_r[1:]):
             vlist.append(hop.inputarg(repr, i+1))
         resulttype = hop.r_result.lowleveltype
-        desc = self._build_desc(vlist, resulttype)
-        v_desc = hop.inputconst(ootype.Void, desc)
-        return hop.genop("direct_call", [v_desc] + vlist, resulttype=resulttype)
+        desc = self._build_desc(vlist)
+        cDesc = hop.inputconst(ootype.Void, desc)
+        return hop.genop("direct_call", [cDesc] + vlist, resulttype=resulttype)
 
 
 
 ## RPython interface definition
 
-class StaticMethodDesc(object):
-    def __init__(self, class_name, method_name, argtypes, resulttype):
-        # TODO: maybe use ootype.StaticMeth for describing signature?
-        self.class_name = class_name
-        self.method_name = method_name
-        self.argtypes = argtypes
-        self.resulttype = resulttype
+
+class _static_meth(object):
+    def __init__(self, TYPE):
+        self._TYPE = TYPE
+
+    def _set_attrs(self, cls, name):
+        self._cls = cls
+        self._name = name
+
+    def _get_desc(self, ARGS):
+        assert ARGS == self._TYPE.ARGS
+        return self
+
+
+class _overloaded_static_meth(ootype._overloaded_mixin):
+    def __init__(self, *overloadings):
+        self._overloadings = overloadings
+        self._check_overloadings()
+
+    def _set_attrs(self, cls, name):
+        for meth in self._overloadings:
+            meth._set_attrs(cls, name)
+
+    def _get_desc(self, ARGS):
+        meth = self._resolve_overloading(ARGS)
+        assert isinstance(meth, _static_meth)
+        return meth._get_desc(ARGS)
 
 
 class CliClass(object):
     def __init__(self, INSTANCE, static_methods):
+        self._name = INSTANCE._name
         self._INSTANCE = INSTANCE
         self._static_methods = static_methods
+        for name, meth in static_methods.iteritems():
+            meth._set_attrs(self, name)
 
     def __repr__(self):
         return '<%s>' % (self,)
@@ -101,15 +124,15 @@
     def __str__(self):
         return '%s(%s)' % (self.__class__.__name__, self._INSTANCE._name)
 
-    def _lookup(self, meth_name, args_s):
-        # TODO: handle conversion
-        overloads = self._static_methods[meth_name]
-        argtypes = tuple([annmodel.annotation_to_lltype(arg_s) for arg_s in args_s])
-        return argtypes, overloads[argtypes]
+    def _lookup(self, meth_name, ARGS):
+        meth = self._static_methods[meth_name]
+        return meth._get_desc(ARGS)
 
     def _ann_static_method(self, meth_name, args_s):
-        argtypes, rettype = self._lookup(meth_name, args_s)
-        return annmodel.lltype_to_annotation(rettype)
+        ARGS = tuple([annmodel.annotation_to_lltype(arg_s) for arg_s in args_s])
+        desc = self._lookup(meth_name, ARGS)
+        RESULT = desc._TYPE.RESULT        
+        return annmodel.lltype_to_annotation(RESULT)
 
 
 class Entry(ExtRegistryEntry):
@@ -135,12 +158,17 @@
                              })
 StringBuilder = CliClass(STRING_BUILDER, {})
 
-CONSOLE = NativeInstance('[mscorlib]', 'System', 'Console', ootype.ROOT, {}, {})
-Console = CliClass(CONSOLE, {'WriteLine': {(ootype.String,): ootype.Void,
-                                          (ootype.Signed,): ootype.Void}})
+##CONSOLE = NativeInstance('[mscorlib]', 'System', 'Console', ootype.ROOT, {}, {})
+##Console = CliClass(CONSOLE, {'WriteLine': {(ootype.String,): ootype.Void,
+##                                           (ootype.Signed,): ootype.Void}})
+
 MATH = NativeInstance('[mscorlib]', 'System', 'Math', ootype.ROOT, {}, {})
-Math = CliClass(MATH, {'Abs': {(ootype.Signed,): ootype.Signed,
-                               (ootype.Float,): ootype.Float}})
+Math = CliClass(MATH,
+                {'Abs': _overloaded_static_meth(_static_meth(StaticMethod([ootype.Signed], ootype.Signed)),
+                                                _static_meth(StaticMethod([ootype.Float], ootype.Float)))
+                 })
+
+
 
 ARRAY_LIST = NativeInstance('[mscorlib]', 'System.Collections', 'ArrayList', ootype.ROOT, {},
                             {'Add': meth(Meth([ootype.ROOT], ootype.Signed)),

Modified: pypy/dist/pypy/translator/cli/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/cli/metavm.py	(original)
+++ pypy/dist/pypy/translator/cli/metavm.py	Fri Oct 20 18:28:16 2006
@@ -4,14 +4,14 @@
      PushAllArgs, StoreResult, GetField, SetField, DownCast
 from pypy.translator.cli.comparer import EqualityComparer
 from pypy.translator.cli.cts import WEAKREF
-from pypy.translator.cli.dotnet import StaticMethodDesc
+from pypy.translator.cli.dotnet import _static_meth
 
 STRING_HELPER_CLASS = '[pypylib]pypy.runtime.String'
 
 class _Call(MicroInstruction):
     def render(self, generator, op):
         callee = op.args[0].value
-        if isinstance(callee, StaticMethodDesc):
+        if isinstance(callee, _static_meth):
             self._render_native_function(generator, callee, op.args)
         else:
             graph = callee.graph
@@ -25,10 +25,10 @@
         for func_arg in args[1:]: # push parameters
             generator.load(func_arg)
         cts = generator.cts
-        ret_type = cts.lltype_to_cts(funcdesc.resulttype)
-        arg_types = [cts.lltype_to_cts(arg) for arg in funcdesc.argtypes if arg is not ootype.Void]
+        ret_type = cts.lltype_to_cts(funcdesc._TYPE.RESULT)
+        arg_types = [cts.lltype_to_cts(arg) for arg in funcdesc._TYPE.ARGS if arg is not ootype.Void]
         arg_list = ', '.join(arg_types)
-        signature = '%s %s::%s(%s)' % (ret_type, funcdesc.class_name, funcdesc.method_name, arg_list)
+        signature = '%s %s::%s(%s)' % (ret_type, funcdesc._cls._name, funcdesc._name, arg_list)
         generator.call_signature(signature)
 
     def _render_function(self, generator, graph, args):

Modified: pypy/dist/pypy/translator/cli/test/test_dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_dotnet.py	Fri Oct 20 18:28:16 2006
@@ -23,6 +23,15 @@
         assert s.cli_class is Math
         assert s.meth_name == 'Abs'
 
+    def test_staticmeth_call_ann(self):
+        def fn1():
+            return Math.Abs(42)
+        def fn2():
+            return Math.Abs(42.5)
+        a = RPythonAnnotator()
+        assert type(a.build_types(fn1, [])) is annmodel.SomeInteger
+        assert type(a.build_types(fn2, [])) is annmodel.SomeFloat
+
     def test_new_instance_ann(self):
         def fn():
             return ArrayList()
@@ -32,12 +41,12 @@
         assert isinstance(s.ootype, NativeInstance)
         assert s.ootype._name == '[mscorlib]System.Collections.ArrayList'
 
-    def test_static_method_call(self):
+    def test_staticmeth_call(self):
         def fn(x):
             return Math.Abs(x)
         assert self.interpret(fn, [-42]) == 42
 
-    def test_static_method_overload(self):
+    def test_staticmeth_overload(self):
         def fn(x, y):
             return Math.Abs(x), Math.Abs(y)
         res = self.interpret(fn, [-42, -42.5])



More information about the Pypy-commit mailing list