Commits

Ronan Lamy committed 6c95c23

auto-register HLOperation classes

Comments (0)

Files changed (2)

rpython/flowspace/model.py

     return Constant(obj)
 
 class SpaceOperation(object):
-    __slots__ = "opname args result offset".split()
 
     def __init__(self, opname, args, result, offset=-1):
         self.opname = intern(opname)      # operation name

rpython/flowspace/operation.py

 
 func2op = {}
 
+class HLOperationMeta(type):
+    def __init__(cls, name, bases, attrdict):
+        type.__init__(cls, name, bases, attrdict)
+        if hasattr(cls, 'opname'):
+            setattr(op, cls.opname, cls)
+
 class HLOperation(SpaceOperation):
+    __metaclass__ = HLOperationMeta
     pure = False
 
     def __init__(self, *args):
 def add_operator(name, arity, pyfunc=None, pure=False, ovf=False):
     operator_func = getattr(operator, name, None)
     base_cls = PureOperation if pure else HLOperation
-    cls = type(name, (base_cls,), {'opname': name, 'arity': arity,
+    cls = HLOperationMeta(name, (base_cls,), {'opname': name, 'arity': arity,
                                    'can_overflow': ovf, 'canraise': []})
-    setattr(op, name, cls)
     if pyfunc is not None:
         func2op[pyfunc] = cls
     if operator_func:
         self.args = [w_base, w_exponent, w_mod]
         self.result = Variable()
         self.offset = -1
-op.pow = Pow
 
 
 class Iter(HLOperation):
             iterable = w_iterable.value
             if isinstance(iterable, unrolling_iterable):
                 return const(iterable.get_unroller())
-op.iter = Iter
 
 class Next(HLOperation):
     opname = 'next'
         w_item = frame.do_op(self)
         frame.guessexception([StopIteration, RuntimeError], force=True)
         return w_item
-op.next = Next
 
 class GetAttr(HLOperation):
     opname = 'getattr'
                 return const(result)
             except WrapException:
                 pass
-op.getattr = GetAttr
 
 class CallOp(HLOperation):
     @property
 
 class SimpleCall(CallOp):
     opname = 'simple_call'
-op.simple_call = SimpleCall
-
 
 class CallArgs(CallOp):
     opname = 'call_args'
-op.call_args = CallArgs
 
 # Other functions that get directly translated to SpaceOperators
 func2op[type] = op.type