Commit 3976a763 authored by Ronan Lamy's avatar Ronan Lamy
Browse files

start splitting SomePBC and SomeNone

parent 3bd72bcbe792
......@@ -9,8 +9,8 @@ from rpython.annotator.model import (
SomeObject, SomeInteger, SomeBool, s_Bool, SomeString, SomeChar, SomeList,
SomeDict, SomeOrderedDict, SomeUnicodeCodePoint, SomeUnicodeString,
SomeTuple, SomeImpossibleValue, s_ImpossibleValue, SomeInstance,
SomeBuiltinMethod, SomeIterator, SomePBC, SomeFloat, s_None, SomeByteArray,
SomeWeakRef, SomeSingleFloat,
SomeBuiltinMethod, SomeIterator, SomePBC, SomeNone, SomeFloat, s_None,
SomeByteArray, SomeWeakRef, SomeSingleFloat,
SomeLongFloat, SomeType, SomeConstantType, unionof, UnionError,
read_can_only_throw, add_knowntypedata,
merge_knowntypedata,)
......@@ -773,19 +773,13 @@ def _make_none_union(classname, constructor_args='', glob=None):
glob = globals()
loc = locals()
source = py.code.Source("""
class __extend__(pairtype(%(classname)s, SomePBC)):
def union((obj, pbc)):
if pbc.isNone():
return %(classname)s(%(constructor_args)s)
else:
raise UnionError(pbc, obj)
class __extend__(pairtype(%(classname)s, SomeNone)):
def union((obj, none)):
return %(classname)s(%(constructor_args)s)
class __extend__(pairtype(SomePBC, %(classname)s)):
def union((pbc, obj)):
if pbc.isNone():
return %(classname)s(%(constructor_args)s)
else:
raise UnionError(pbc, obj)
class __extend__(pairtype(SomeNone, %(classname)s)):
def union((none, obj)):
return %(classname)s(%(constructor_args)s)
""" % loc)
exec source.compile() in glob
......
"""
Type inference for user-defined classes.
"""
from rpython.annotator.model import SomePBC, s_ImpossibleValue, unionof
from rpython.annotator.model import SomeInteger, SomeTuple, SomeString, AnnotatorError
from rpython.annotator.model import (
SomePBC, s_ImpossibleValue, unionof, s_None, SomeInteger, SomeTuple,
SomeString, AnnotatorError)
from rpython.annotator import description
......@@ -351,8 +352,10 @@ class ClassDef(object):
if uplookup is not None:
d.append(updesc.bind_self(self, flags))
if d or pbc.can_be_None:
if d:
return SomePBC(d, can_be_None=pbc.can_be_None)
elif pbc.can_be_None:
return s_None
else:
return s_ImpossibleValue
......
......@@ -424,36 +424,32 @@ class SomePBC(SomeObject):
immutable = True
def __init__(self, descriptions, can_be_None=False, subset_of=None):
assert descriptions
# descriptions is a set of Desc instances
descriptions = set(descriptions)
self.descriptions = descriptions
self.can_be_None = can_be_None
self.subset_of = subset_of
self.simplify()
if self.isNone():
self.knowntype = type(None)
self.const = None
else:
knowntype = reduce(commonbase,
[x.knowntype for x in descriptions])
if knowntype == type(Exception):
knowntype = type
if knowntype != object:
self.knowntype = knowntype
if len(descriptions) == 1 and not can_be_None:
# hack for the convenience of direct callers to SomePBC():
# only if there is a single object in descriptions
desc, = descriptions
if desc.pyobj is not None:
self.const = desc.pyobj
elif len(descriptions) > 1:
from rpython.annotator.description import ClassDesc
if self.getKind() is ClassDesc:
# a PBC of several classes: enforce them all to be
# built, without support for specialization. See
# rpython/test/test_rpbc.test_pbc_of_classes_not_all_used
for desc in descriptions:
desc.getuniqueclassdef()
knowntype = reduce(commonbase, [x.knowntype for x in descriptions])
if knowntype == type(Exception):
knowntype = type
if knowntype != object:
self.knowntype = knowntype
if len(descriptions) == 1 and not can_be_None:
# hack for the convenience of direct callers to SomePBC():
# only if there is a single object in descriptions
desc, = descriptions
if desc.pyobj is not None:
self.const = desc.pyobj
elif len(descriptions) > 1:
from rpython.annotator.description import ClassDesc
if self.getKind() is ClassDesc:
# a PBC of several classes: enforce them all to be
# built, without support for specialization. See
# rpython/test/test_rpbc.test_pbc_of_classes_not_all_used
for desc in descriptions:
desc.getuniqueclassdef()
def any_description(self):
return iter(self.descriptions).next()
......@@ -466,32 +462,24 @@ class SomePBC(SomeObject):
kinds.add(x.__class__)
if len(kinds) > 1:
raise AnnotatorError("mixing several kinds of PBCs: %r" % kinds)
if not kinds:
raise ValueError("no 'kind' on the 'None' PBC")
return kinds.pop()
def simplify(self):
if self.descriptions:
# We check that the set only contains a single kind of Desc instance
kind = self.getKind()
# then we remove unnecessary entries in self.descriptions:
# some MethodDescs can be 'shadowed' by others
if len(self.descriptions) > 1:
kind.simplify_desc_set(self.descriptions)
else:
assert self.can_be_None, "use s_ImpossibleValue"
# We check that the set only contains a single kind of Desc instance
kind = self.getKind()
# then we remove unnecessary entries in self.descriptions:
# some MethodDescs can be 'shadowed' by others
if len(self.descriptions) > 1:
kind.simplify_desc_set(self.descriptions)
def isNone(self):
return len(self.descriptions) == 0
return False
def can_be_none(self):
return self.can_be_None
def nonnoneify(self):
if self.isNone():
return s_ImpossibleValue
else:
return SomePBC(self.descriptions, can_be_None=False)
return SomePBC(self.descriptions, can_be_None=False)
def fmt_descriptions(self, pbis):
if hasattr(self, 'const'):
......@@ -527,6 +515,10 @@ class SomeNone(SomePBC):
def is_immutable_constant(self):
return True
def nonnoneify(self):
return s_ImpossibleValue
class SomeConstantType(SomePBC):
can_be_None = False
subset_of = None
......
......@@ -8,7 +8,7 @@ from rpython.flowspace.operation import op
from rpython.annotator.model import (SomeObject, SomeInteger, SomeBool,
SomeString, SomeChar, SomeList, SomeDict, SomeTuple, SomeImpossibleValue,
SomeUnicodeCodePoint, SomeInstance, SomeBuiltin, SomeBuiltinMethod,
SomeFloat, SomeIterator, SomePBC, SomeType, s_ImpossibleValue,
SomeFloat, SomeIterator, SomePBC, SomeNone, SomeType, s_ImpossibleValue,
s_Bool, s_None, unionof, add_knowntypedata,
HarmlesslyBlocked, SomeWeakRef, SomeUnicodeString, SomeByteArray)
from rpython.annotator.bookkeeper import getbookkeeper, immutablevalue
......@@ -765,6 +765,10 @@ class __extend__(SomePBC):
# This should probably never happen
raise AnnotatorError("Cannot call len on a pbc")
class __extend__(SomeNone):
def bind_callables_under(self, classdef, name):
return self
#_________________________________________
# weakrefs
......
from rpython.annotator import model as annmodel
from rpython.tool.pairtype import pairtype
from rpython.annotator.binaryop import _make_none_union, SomePBC # SomePBC needed by _make_none_union
from rpython.annotator.binaryop import _make_none_union, SomeNone # SomeNone needed by _make_none_union
from rpython.annotator.bookkeeper import getbookkeeper
from rpython.rtyper.extregistry import ExtRegistryEntry
from rpython.rtyper.annlowlevel import cachedtype
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment