def compute_result_annotation(self, type_s, length_s): from pypy.translator.cli.query import get_cli_class assert type_s.is_constant() assert isinstance(length_s, SomeInteger) TYPE = type_s.const._INSTANCE fullname = '%s.%s[]' % (TYPE._namespace, TYPE._classname) cliArray = get_cli_class(fullname) return SomeOOInstance(cliArray._INSTANCE)
def compute_result_annotation(self, s_value, s_type): if isinstance(s_type.const, ootype.OOType): TYPE = s_type.const else: cliClass = s_type.const TYPE = cliClass._INSTANCE assert ootype.isSubclass(TYPE, s_value.ootype) return SomeOOInstance(TYPE)
def lltype_to_annotation(cls, TYPE): if isinstance(TYPE, NativeInstance): return SomeOOInstance(TYPE) elif TYPE is ootype.Char: return SomeChar() elif TYPE is ootype.String: return SomeString(can_be_None=True) else: return lltype_to_annotation(TYPE)
def compute_result_annotation(self, type_s, *args_s): from pypy.translator.cli.query import get_cli_class assert type_s.is_constant() TYPE = type_s.const._INSTANCE for i, arg_s in enumerate(args_s): if TYPE is not arg_s.ootype: raise TypeError, 'Wrong type of arg #%d: %s expected, %s found' % \ (i, TYPE, arg_s.ootype) fullname = '%s.%s[]' % (TYPE._namespace, TYPE._classname) cliArray = get_cli_class(fullname) return SomeOOInstance(cliArray._INSTANCE)
def cast_from_object(T, obj): TYPE = T.const if TYPE is ootype.Object: return SomeOOObject() elif TYPE is ootype.Class: return SomeOOClass(ootype.ROOT) # ??? elif isinstance(TYPE, ootype.StaticMethod): return SomeOOStaticMeth(TYPE) elif isinstance(TYPE, ootype.OOType): return SomeOOInstance(TYPE) else: raise AnnotatorError, 'Cannot cast Object to %s' % TYPE
def compute_result_annotation(self, x_s, type_s): assert isinstance(x_s, SomeOOInstance) assert isinstance(x_s.ootype, NativeInstance) assert type_s.is_constant() TYPE = type_s.const if isinstance(TYPE, (type, types.ClassType)): # it's a user-defined class, so we return SomeInstance # can_be_None == True because it can always return None, if it fails classdef = self.bookkeeper.getuniqueclassdef(TYPE) return SomeInstance(classdef, can_be_None=True) elif isinstance(TYPE, ootype.StaticMethod): return SomeOOStaticMeth(TYPE) elif isinstance(TYPE, ootype.OOType): return SomeOOInstance(TYPE) else: assert TYPE in BOXABLE_TYPES return OverloadingResolver.lltype_to_annotation(TYPE)
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)
def oonewarray(s_type, length): assert s_type.is_constant() return SomeOOInstance(s_type.const)
def runtimenew(c): assert isinstance(c, SomeOOClass) if c.ootype is None: return s_ImpossibleValue # can't call runtimenew(NULL) else: return SomeOOInstance(c.ootype)
def union((r1, r2)): common = ootype.commonBaseclass(r1.ootype, r2.ootype) assert common is not None, 'Mixing of incompatible instances %r, %r' % ( r1.ootype, r2.ootype) return SomeOOInstance(common, can_be_None=r1.can_be_None or r2.can_be_None)
def new(I): assert I.is_constant() i = ootype.new(I.const) r = SomeOOInstance(ootype.typeOf(i)) return r
def compute_annotation(self): return SomeOOInstance(CLR.System.Reflection.FieldInfo._INSTANCE)
def getitem((ooinst, index)): if ooinst.ootype._isArray: return SomeOOInstance(ooinst.ootype._ELEMENT) return s_ImpossibleValue
def compute_result_annotation(self, s_type, s_value): assert s_type.is_constant() T = s_type.const assert isinstance(T, ootype.Record) can_be_None = getattr(s_value, 'can_be_None', False) return SomeOOInstance(T, can_be_None)
def compute_result_annotation(self, s_const): assert s_const.is_constant() return SomeOOInstance(CLR.System.Reflection.FieldInfo._INSTANCE)
def compute_result_annotation(self): return SomeOOInstance(self.instance._INSTANCE)
def compute_result_annotation(self, s_value): assert isinstance(s_value, annmodel.SomeOOObject) assert s_value.ootype is ootype.Object return SomeOOInstance(CLR.System.Object._INSTANCE)
def compute_result_annotation(self, s_value): from pypy.translator.cli.query import get_cli_class cliType = get_cli_class('System.EventHandler') return SomeOOInstance(cliType._INSTANCE)
def compute_result_annotation(self, cliClass_s): from pypy.translator.cli.query import get_cli_class assert cliClass_s.is_constant() cliType = get_cli_class('System.Type') return SomeOOInstance(cliType._INSTANCE)
def simple_call(self, *s_args): assert self.is_constant() return SomeOOInstance(self.const._INSTANCE)
def compute_result_annotation(self, x_s): can_be_None = getattr(x_s, 'can_be_None', False) return SomeOOInstance(CLR.System.Object._INSTANCE, can_be_None=can_be_None)
def compute_result_annotation(self, exc_s): assert isinstance(exc_s, SomeInstance) cls = exc_s.classdef.classdesc.pyobj assert issubclass(cls, Exception) NATIVE_INSTANCE = cls._rpython_hints['NATIVE_INSTANCE'] return SomeOOInstance(NATIVE_INSTANCE)
def immutablevalue(self, x, need_const=True): """The most precise SomeValue instance that contains the immutable value x.""" # convert unbound methods to the underlying function if hasattr(x, 'im_self') and x.im_self is None: x = x.im_func assert not hasattr(x, 'im_self') if x is sys: # special case constant sys to someobject return SomeObject() tp = type(x) if issubclass(tp, Symbolic): # symbolic constants support result = x.annotation() result.const_box = Constant(x) return result if tp is bool: result = SomeBool() elif tp is int: result = SomeInteger(nonneg=x >= 0) elif tp is long: if -sys.maxint - 1 <= x <= sys.maxint: x = int(x) result = SomeInteger(nonneg=x >= 0) else: raise Exception("seeing a prebuilt long (value %s)" % hex(x)) elif issubclass(tp, str): # py.lib uses annotated str subclasses if len(x) == 1: result = SomeChar() else: result = SomeString() elif tp is unicode: if len(x) == 1: result = SomeUnicodeCodePoint() else: result = SomeUnicodeString() elif tp is tuple: result = SomeTuple( items=[self.immutablevalue(e, need_const) for e in x]) elif tp is float: result = SomeFloat() elif tp is list: if need_const: key = Constant(x) try: return self.immutable_cache[key] except KeyError: result = SomeList(ListDef(self, s_ImpossibleValue)) self.immutable_cache[key] = result for e in x: result.listdef.generalize(self.immutablevalue(e)) result.const_box = key return result else: listdef = ListDef(self, s_ImpossibleValue) for e in x: listdef.generalize(self.immutablevalue(e, False)) result = SomeList(listdef) elif tp is dict or tp is r_dict: if need_const: key = Constant(x) try: return self.immutable_cache[key] except KeyError: result = SomeDict( DictDef(self, s_ImpossibleValue, s_ImpossibleValue, is_r_dict=tp is r_dict)) self.immutable_cache[key] = result if tp is r_dict: s_eqfn = self.immutablevalue(x.key_eq) s_hashfn = self.immutablevalue(x.key_hash) result.dictdef.dictkey.update_rdict_annotations( s_eqfn, s_hashfn) seen_elements = 0 while seen_elements != len(x): items = x.items() for ek, ev in items: result.dictdef.generalize_key( self.immutablevalue(ek)) result.dictdef.generalize_value( self.immutablevalue(ev)) result.dictdef.seen_prebuilt_key(ek) seen_elements = len(items) # if the dictionary grew during the iteration, # start over again result.const_box = key return result else: dictdef = DictDef(self, s_ImpossibleValue, s_ImpossibleValue, is_r_dict=tp is r_dict) if tp is r_dict: s_eqfn = self.immutablevalue(x.key_eq) s_hashfn = self.immutablevalue(x.key_hash) dictdef.dictkey.update_rdict_annotations(s_eqfn, s_hashfn) for ek, ev in x.iteritems(): dictdef.generalize_key(self.immutablevalue(ek, False)) dictdef.generalize_value(self.immutablevalue(ev, False)) dictdef.seen_prebuilt_key(ek) result = SomeDict(dictdef) elif tp is weakref.ReferenceType: x1 = x() if x1 is None: result = SomeWeakRef(None) # dead weakref else: s1 = self.immutablevalue(x1) assert isinstance(s1, SomeInstance) result = SomeWeakRef(s1.classdef) elif ishashable(x) and x in BUILTIN_ANALYZERS: _module = getattr(x, "__module__", "unknown") result = SomeBuiltin(BUILTIN_ANALYZERS[x], methodname="%s.%s" % (_module, x.__name__)) elif extregistry.is_registered(x, self.policy): entry = extregistry.lookup(x, self.policy) result = entry.compute_annotation_bk(self) elif isinstance(x, lltype._ptr): result = SomePtr(lltype.typeOf(x)) elif isinstance(x, llmemory.fakeaddress): result = SomeAddress() elif isinstance(x, ootype._static_meth): result = SomeOOStaticMeth(ootype.typeOf(x)) elif isinstance(x, ootype._class): result = SomeOOClass(x._INSTANCE) # NB. can be None elif isinstance(x, ootype.instance_impl): # XXX result = SomeOOInstance(ootype.typeOf(x)) elif isinstance(x, (ootype._record, ootype._string)): result = SomeOOInstance(ootype.typeOf(x)) elif isinstance(x, (ootype._object)): result = SomeOOObject() elif callable(x): if hasattr(x, 'im_self') and hasattr(x, 'im_func'): # on top of PyPy, for cases like 'l.append' where 'l' is a # global constant list, the find_method() returns non-None s_self = self.immutablevalue(x.im_self, need_const) result = s_self.find_method(x.im_func.__name__) elif hasattr(x, '__self__') and x.__self__ is not None: # for cases like 'l.append' where 'l' is a global constant list s_self = self.immutablevalue(x.__self__, need_const) result = s_self.find_method(x.__name__) if result is None: result = SomeObject() else: result = None if result is None: if (self.annotator.policy.allow_someobjects and getattr(x, '__module__', None) == '__builtin__' # XXX note that the print support functions are __builtin__ and tp not in (types.FunctionType, types.MethodType)): result = SomeObject() result.knowntype = tp # at least for types this needs to be correct else: result = SomePBC([self.getdesc(x)]) elif hasattr(x, '_freeze_') and x._freeze_(): # user-defined classes can define a method _freeze_(), which # is called when a prebuilt instance is found. If the method # returns True, the instance is considered immutable and becomes # a SomePBC(). Otherwise it's just SomeInstance(). result = SomePBC([self.getdesc(x)]) elif hasattr(x, '__class__') \ and x.__class__.__module__ != '__builtin__': self.see_mutable(x) result = SomeInstance(self.getuniqueclassdef(x.__class__)) elif x is None: return s_None else: result = SomeObject() if need_const: result.const = x return result
def compute_result_annotation(self, s_value): T = s_value.ootype assert isinstance(T, ootype.Record) can_be_None = getattr(s_value, 'can_be_None', False) return SomeOOInstance(CLR.System.Object._INSTANCE, can_be_None=can_be_None)