[pypy-svn] r13020 - pypy/branch/rpython-refactoring
arigo at codespeak.net
arigo at codespeak.net
Thu Jun 2 22:55:14 CEST 2005
Author: arigo
Date: Thu Jun 2 22:55:14 2005
New Revision: 13020
Added:
pypy/branch/rpython-refactoring/rmodel.py (contents, props changed)
Log:
Forgot to check this file in.
Added: pypy/branch/rpython-refactoring/rmodel.py
==============================================================================
--- (empty file)
+++ pypy/branch/rpython-refactoring/rmodel.py Thu Jun 2 22:55:14 2005
@@ -0,0 +1,113 @@
+from pypy.annotation.pairtype import pair, pairtype, extendabletype
+from pypy.annotation import model as annmodel
+from pypy.objspace.flow.model import Constant
+from pypy.rpython.lltype import Void, Bool
+
+
+class Repr:
+ """ An instance of Repr is associated with each instance of SomeXxx.
+ It defines the chosen representation for the SomeXxx. The Repr subclasses
+ generally follows the SomeXxx subclass hierarchy, but there are numerous
+ exceptions. For example, the anotator uses SomeIter for any iterator, but
+ we need different representations according to the type of container we are
+ iterating over.
+ """
+ __metaclass__ = extendabletype
+
+ def __repr__(self):
+ return '<%s %s>' % (self.__class__.__name__, self.lowleveltype)
+
+ # default implementation of some operations
+
+ def rtype_getattr(self, hop):
+ s_attr = hop.args_s[1]
+ if s_attr.is_constant() and isinstance(s_attr.const, str):
+ attr = s_attr.const
+ s_obj = hop.args_s[0]
+ try:
+ s_obj.find_method(attr) # just to check it is here
+ except AttributeError:
+ raise TyperError("no method %s on %r" % (attr, s_obj))
+ else:
+ # implement methods (of a known name) as just their 'self'
+ return hop.inputarg(self, arg=0)
+ else:
+ raise TyperError("getattr() with a non-constant attribute name")
+
+ def rtype_nonzero(self, hop):
+ return self.rtype_is_true(hop) # can call a subclass' rtype_is_true()
+
+ def rtype_is_true(self, hop):
+ try:
+ vlen = self.rtype_len(hop)
+ except MissingRTypeOperation:
+ return hop.inputconst(Bool, True)
+ else:
+ return hop.genop('int_is_true', [vlen], resulttype=Bool)
+
+# ____________________________________________________________
+
+class TyperError(Exception):
+ def __str__(self):
+ result = Exception.__str__(self)
+ if hasattr(self, 'where'):
+ result += '\n.. %r\n.. %r' % self.where
+ return result
+
+class MissingRTypeOperation(TyperError):
+ pass
+
+def missing_rtype_operation(self, hop):
+ raise MissingRTypeOperation("unimplemented operation: '%s' on %r" % (
+ hop.spaceop.opname, self))
+
+def setattr_default(obj, attr, value):
+ if not hasattr(obj, attr):
+ setattr(obj, attr, value)
+
+for opname in annmodel.UNARY_OPERATIONS:
+ setattr_default(Repr, 'rtype_' + opname, missing_rtype_operation)
+for opname in annmodel.BINARY_OPERATIONS:
+ setattr_default(pairtype(Repr, Repr),
+ 'rtype_' + opname, missing_rtype_operation)
+
+
+class __extend__(pairtype(Repr, Repr)):
+ def convert_from_to((r_from, r_to), v, llops):
+ return NotImplemented
+
+# ____________________________________________________________
+# Primitive Repr classes, in the same hierarchical order as
+# the corresponding SomeObjects
+
+class FloatRepr(Repr):
+ lowleveltype = Float
+
+class IntegerRepr(FloatRepr):
+ lowleveltype = Signed
+
+class BoolRepr(IntegerRepr):
+ lowleveltype = Bool
+
+class StringRepr(Repr):
+ pass
+
+class CharRepr(StringRepr):
+ lowleveltype = Char
+
+# ____________________________________________________________
+
+def inputconst(reqtype, value):
+ """Return a Constant with the given value, of the requested type,
+ which can be a Repr instance or a low-level type.
+ """
+ if isinstance(reqtype, Repr):
+ reqtype = reqtype.lowleveltype
+ # Void Constants can hold any value;
+ # non-Void Constants must hold a correctly ll-typed value
+ # XXX ---- not enforced yet ----
+ #if reqtype is not Void:
+ # assert typeOf(value) == reqtype
+ c = Constant(value)
+ c.concretetype = reqtype
+ return c
More information about the Pypy-commit
mailing list