[pypy-svn] r49813 - in pypy/dist/pypy: annotation rpython/ootypesystem rpython/ootypesystem/test
antocuni at codespeak.net
antocuni at codespeak.net
Sat Dec 15 12:35:34 CET 2007
Author: antocuni
Date: Sat Dec 15 12:35:33 2007
New Revision: 49813
Modified:
pypy/dist/pypy/annotation/builtin.py
pypy/dist/pypy/rpython/ootypesystem/ootype.py
pypy/dist/pypy/rpython/ootypesystem/rbuiltin.py
pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py
Log:
add annotator/rtyper support for oodowncast and ooupcast
Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py (original)
+++ pypy/dist/pypy/annotation/builtin.py Sat Dec 15 12:35:33 2007
@@ -16,6 +16,7 @@
from pypy.annotation.bookkeeper import getbookkeeper
from pypy.annotation import description
from pypy.objspace.flow.model import Constant
+from pypy.tool.error import AnnotatorError
import pypy.rlib.rarithmetic
import pypy.rlib.objectmodel
@@ -547,6 +548,20 @@
assert isinstance(i, SomeOOInstance)
return SomeInteger()
+def ooupcast(I, i):
+ assert isinstance(I.const, ootype.Instance)
+ if ootype.isSubclass(i.ootype, I.const):
+ return SomeOOInstance(I.const)
+ else:
+ raise AnnotatorError, 'Cannot cast %s to %s' % (i.ootype, I.const)
+
+def oodowncast(I, i):
+ assert isinstance(I.const, ootype.Instance)
+ if ootype.isSubclass(I.const, i.ootype):
+ return SomeOOInstance(I.const)
+ else:
+ raise AnnotatorError, 'Cannot cast %s to %s' % (i.ootype, I.const)
+
BUILTIN_ANALYZERS[ootype.instanceof] = instanceof
BUILTIN_ANALYZERS[ootype.new] = new
BUILTIN_ANALYZERS[ootype.null] = null
@@ -554,6 +569,8 @@
BUILTIN_ANALYZERS[ootype.classof] = classof
BUILTIN_ANALYZERS[ootype.subclassof] = subclassof
BUILTIN_ANALYZERS[ootype.ooidentityhash] = ooidentityhash
+BUILTIN_ANALYZERS[ootype.ooupcast] = ooupcast
+BUILTIN_ANALYZERS[ootype.oodowncast] = oodowncast
#________________________________
# weakrefs
Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py Sat Dec 15 12:35:33 2007
@@ -263,11 +263,12 @@
# We try to keep Record as similar to Instance as possible, so backends
# can treat them polymorphically, if they choose to do so.
- def __init__(self, fields):
+ def __init__(self, fields, _hints={}):
self._fields = frozendict()
for name, ITEMTYPE in fields.items():
self._fields[name] = ITEMTYPE, ITEMTYPE._defl()
self._null = _null_record(self)
+ self._hints = frozendict(_hints)
def _defl(self):
return self._null
Modified: pypy/dist/pypy/rpython/ootypesystem/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rbuiltin.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/rbuiltin.py Sat Dec 15 12:35:33 2007
@@ -40,6 +40,18 @@
return hop.genop('ooidentityhash', vlist,
resulttype = ootype.Signed)
+def rtype_ooupcast(hop):
+ assert isinstance(hop.args_s[0].const, ootype.Instance)
+ assert isinstance(hop.args_s[1], annmodel.SomeOOInstance)
+ v_inst = hop.inputarg(hop.args_r[1], arg=1)
+ return hop.genop('ooupcast', [v_inst], resulttype = hop.r_result.lowleveltype)
+
+def rtype_oodowncast(hop):
+ assert isinstance(hop.args_s[0].const, ootype.Instance)
+ assert isinstance(hop.args_s[1], annmodel.SomeOOInstance)
+ v_inst = hop.inputarg(hop.args_r[1], arg=1)
+ return hop.genop('oodowncast', [v_inst], resulttype = hop.r_result.lowleveltype)
+
def rtype_builtin_isinstance(hop):
if hop.s_result.is_constant():
return hop.inputconst(ootype.Bool, hop.s_result.const)
@@ -98,6 +110,8 @@
BUILTIN_TYPER[ootype.subclassof] = rtype_subclassof
BUILTIN_TYPER[ootype.runtimenew] = rtype_runtimenew
BUILTIN_TYPER[ootype.ooidentityhash] = rtype_ooidentityhash
+BUILTIN_TYPER[ootype.ooupcast] = rtype_ooupcast
+BUILTIN_TYPER[ootype.oodowncast] = rtype_oodowncast
BUILTIN_TYPER[isinstance] = rtype_builtin_isinstance
BUILTIN_TYPER[objectmodel.r_dict] = rtype_r_dict
BUILTIN_TYPER[objectmodel.instantiate] = rtype_instantiate
Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py Sat Dec 15 12:35:33 2007
@@ -9,6 +9,7 @@
from pypy.translator.translator import TranslationContext, graphof
from pypy.rpython.test.test_llinterp import interpret
from pypy.rlib.objectmodel import r_dict
+from pypy.tool.error import AnnotatorError
from pypy.rpython.ootypesystem import ooregistry # side effects
def gengraph(f, args=[], viewBefore=False, viewAfter=False, mangle=True):
@@ -300,3 +301,40 @@
res = interpret(oof, [], type_system='ootype')
assert res == 42
+
+def test_ooupcast():
+ A = Instance('A', ootype.ROOT, {})
+ B = Instance('B', A, {})
+ C = Instance('C', ootype.ROOT)
+
+ def fn():
+ b = new(B)
+ return ooupcast(A, b)
+
+ res = interpret(fn, [], type_system='ootype')
+ assert typeOf(res) is A
+
+ def fn():
+ c = new(C)
+ return ooupcast(A, c)
+
+ py.test.raises(AnnotatorError, interpret, fn, [], type_system='ootype')
+
+def test_oodowncast():
+ A = Instance('A', ootype.ROOT, {})
+ B = Instance('B', A, {})
+ C = Instance('C', ootype.ROOT)
+
+ def fn():
+ b = new(B)
+ a = ooupcast(A, b)
+ return oodowncast(B, a)
+
+ res = interpret(fn, [], type_system='ootype')
+ assert typeOf(res) is B
+
+ def fn():
+ c = new(C)
+ return oodowncast(A, c)
+
+ py.test.raises(AnnotatorError, interpret, fn, [], type_system='ootype')
More information about the Pypy-commit
mailing list