Author: Ronan Lamy <[email protected]>
Branch: var-in-Some
Changeset: r71556:fadabd4a7942
Date: 2014-05-17 03:00 +0100
http://bitbucket.org/pypy/pypy/changeset/fadabd4a7942/

Log:    allow registration of specialized annotators for unary operations
        using AnnotatedValues

diff --git a/rpython/flowspace/operation.py b/rpython/flowspace/operation.py
--- a/rpython/flowspace/operation.py
+++ b/rpython/flowspace/operation.py
@@ -13,7 +13,7 @@
 from rpython.flowspace.model import (Constant, WrapException, const, Variable,
                                      SpaceOperation)
 from rpython.flowspace.specialcase import register_flow_sc
-from rpython.annotator.model import SomeTuple
+from rpython.annotator.model import SomeTuple, AnnotatorError
 from rpython.flowspace.specialcase import SPECIAL_CASES
 
 
@@ -53,6 +53,13 @@
         type.__init__(cls, name, bases, attrdict)
         if hasattr(cls, 'opname'):
             setattr(op, cls.opname, cls)
+        cls._registry = {}
+
+    def register(cls, Some_cls):
+        def decorator(func):
+            cls._registry[Some_cls] = func
+        return decorator
+
 
 class HLOperation(SpaceOperation):
     __metaclass__ = HLOperationMeta
@@ -140,11 +147,23 @@
     dispatch = 1
 
     @classmethod
+    def _dispatch(cls, Some_cls):
+        for c in Some_cls.__mro__:
+            try:
+                return cls._registry[c]
+            except KeyError:
+                pass
+        raise AnnotatorError("Unknown operation")
+
+    @classmethod
     def get_specialization(cls, s_arg, *_ignored):
-        impl = getattr(s_arg, cls.opname)
-        def specialized(arg, *other_args):
-            return impl(*[x.ann for x in other_args])
-        return specialized
+        try:
+            impl = getattr(s_arg, cls.opname)
+            def specialized(arg, *other_args):
+                return impl(*[x.ann for x in other_args])
+            return specialized
+        except AttributeError:
+            return cls._dispatch(type(s_arg))
 
 
 class DoubleDispatchMixin(object):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to