| 1 | from rpython.flowspace.model import Constant |
| 2 | from rpython.annotator.model import SomeNone |
| 3 | from rpython.rtyper.rmodel import Repr, TyperError, inputconst |
| 4 | from rpython.rtyper.lltypesystem.lltype import Void, Bool, Ptr, Char |
| 5 | from rpython.rtyper.lltypesystem.llmemory import Address |
| 6 | from rpython.rtyper.rpbc import SmallFunctionSetPBCRepr |
| 7 | from rpython.rtyper.annlowlevel import llstr |
| 8 | from rpython.tool.pairtype import pairtype |
| 9 | |
| 10 | class NoneRepr(Repr): |
| 11 | lowleveltype = Void |
| 12 | |
| 13 | def rtype_bool(self, hop): |
| 14 | return Constant(False, Bool) |
| 15 | |
| 16 | def none_call(self, hop): |
| 17 | raise TyperError("attempt to call constant None") |
| 18 | |
| 19 | def ll_str(self, none): |
| 20 | return llstr("None") |
| 21 | |
| 22 | def get_ll_eq_function(self): |
| 23 | return None |
| 24 | |
| 25 | def get_ll_hash_function(self): |
| 26 | return ll_none_hash |
| 27 | |
| 28 | get_ll_fasthash_function = get_ll_hash_function |
| 29 | |
| 30 | rtype_simple_call = none_call |
| 31 | rtype_call_args = none_call |
| 32 | |
| 33 | none_repr = NoneRepr() |
| 34 | |
| 35 | class __extend__(SomeNone): |
| 36 | def rtyper_makerepr(self, rtyper): |
| 37 | return none_repr |
| 38 | |
| 39 | def rtyper_makekey(self): |
| 40 | return self.__class__, |
| 41 | |
| 42 | def ll_none_hash(_): |
| 43 | return 0 |
| 44 | |
| 45 | |
| 46 | class __extend__(pairtype(Repr, NoneRepr)): |
| 47 | |
| 48 | def convert_from_to((r_from, _), v, llops): |
| 49 | return inputconst(Void, None) |
| 50 | |
| 51 | def rtype_is_((robj1, rnone2), hop): |
| 52 | if hop.s_result.is_constant(): |
| 53 | return hop.inputconst(Bool, hop.s_result.const) |
| 54 | return rtype_is_None(robj1, rnone2, hop) |
| 55 | |
| 56 | class __extend__(pairtype(NoneRepr, Repr)): |
| 57 | |
| 58 | def convert_from_to((_, r_to), v, llops): |
| 59 | return inputconst(r_to, None) |
| 60 | |
| 61 | def rtype_is_((rnone1, robj2), hop): |
| 62 | if hop.s_result.is_constant(): |
| 63 | return hop.inputconst(Bool, hop.s_result.const) |
| 64 | return rtype_is_None(robj2, rnone1, hop, pos=1) |
| 65 | |
| 66 | def rtype_is_None(robj1, rnone2, hop, pos=0): |
| 67 | if isinstance(robj1.lowleveltype, Ptr): |
| 68 | v1 = hop.inputarg(robj1, pos) |
| 69 | return hop.genop('ptr_iszero', [v1], resulttype=Bool) |
| 70 | elif robj1.lowleveltype == Address: |
| 71 | v1 = hop.inputarg(robj1, pos) |
| 72 | cnull = hop.inputconst(Address, robj1.null_instance()) |
| 73 | return hop.genop('adr_eq', [v1, cnull], resulttype=Bool) |
| 74 | elif robj1 == none_repr: |
| 75 | return hop.inputconst(Bool, True) |
| 76 | elif isinstance(robj1, SmallFunctionSetPBCRepr): |
| 77 | if robj1.s_pbc.can_be_None: |
| 78 | v1 = hop.inputarg(robj1, pos) |
| 79 | return hop.genop('char_eq', [v1, inputconst(Char, '\000')], |
| 80 | resulttype=Bool) |
| 81 | else: |
| 82 | return inputconst(Bool, False) |
| 83 | else: |
| 84 | raise TyperError('rtype_is_None of %r' % (robj1)) |