| 1 |
from rpython.flowspace.model import Constant |
| 2 |
from rpython.flowspace.operation import op |
| 3 |
from rpython.annotator import model as annmodel |
| 4 |
from rpython.tool.pairtype import pairtype |
| 5 |
from rpython.annotator.bookkeeper import getbookkeeper |
| 6 |
from rpython.rlib.objectmodel import specialize |
| 7 |
from rpython.rtyper.rmodel import Repr |
| 8 |
from rpython.rtyper.extregistry import ExtRegistryEntry |
| 9 |
from rpython.rtyper.annlowlevel import cachedtype |
| 10 |
from rpython.rtyper.error import TyperError |
| 11 |
|
| 12 |
|
| 13 |
class ControllerEntry(ExtRegistryEntry): |
| 14 |
|
| 15 |
def compute_result_annotation(self, *args_s, **kwds_s): |
| 16 |
controller = self.getcontroller(*args_s, **kwds_s) |
| 17 |
if kwds_s: |
| 18 |
raise TypeError("cannot handle keyword arguments in %s" % ( |
| 19 |
self.new,)) |
| 20 |
s_real_obj = delegate(controller.new, *args_s) |
| 21 |
if s_real_obj == annmodel.s_ImpossibleValue: |
| 22 |
return annmodel.s_ImpossibleValue |
| 23 |
else: |
| 24 |
return SomeControlledInstance(s_real_obj, controller) |
| 25 |
|
| 26 |
def getcontroller(self, *args_s, **kwds_s): |
| 27 |
return self._controller_() |
| 28 |
|
| 29 |
def specialize_call(self, hop, **kwds_i): |
| 30 |
if hop.s_result == annmodel.s_ImpossibleValue: |
| 31 |
raise TyperError("object creation always raises: %s" % ( |
| 32 |
hop.spaceop,)) |
| 33 |
assert not kwds_i |
| 34 |
controller = hop.s_result.controller |
| 35 |
return rtypedelegate(controller.new, hop, revealargs=[], |
| 36 |
revealresult=True) |
| 37 |
|
| 38 |
|
| 39 |
def controlled_instance_box(controller, obj): |
| 40 |
XXX |
| 41 |
|
| 42 |
def controlled_instance_unbox(controller, obj): |
| 43 |
XXX |
| 44 |
|
| 45 |
def controlled_instance_is_box(controller, obj): |
| 46 |
XXX |
| 47 |
|
| 48 |
|
| 49 |
class ControllerEntryForPrebuilt(ExtRegistryEntry): |
| 50 |
|
| 51 |
def compute_annotation(self): |
| 52 |
controller = self.getcontroller() |
| 53 |
real_obj = controller.convert(self.instance) |
| 54 |
s_real_obj = self.bookkeeper.immutablevalue(real_obj) |
| 55 |
return SomeControlledInstance(s_real_obj, controller) |
| 56 |
|
| 57 |
def getcontroller(self): |
| 58 |
return self._controller_() |
| 59 |
|
| 60 |
|
| 61 |
class Controller(object): |
| 62 |
__metaclass__ = cachedtype |
| 63 |
can_be_None = False |
| 64 |
|
| 65 |
def _freeze_(self): |
| 66 |
return True |
| 67 |
|
| 68 |
@specialize.arg(0) |
| 69 |
def box(self, obj): |
| 70 |
return controlled_instance_box(self, obj) |
| 71 |
|
| 72 |
@specialize.arg(0) |
| 73 |
def unbox(self, obj): |
| 74 |
return controlled_instance_unbox(self, obj) |
| 75 |
|
| 76 |
@specialize.arg(0) |
| 77 |
def is_box(self, obj): |
| 78 |
return controlled_instance_is_box(self, obj) |
| 79 |
|
| 80 |
@specialize.arg(0, 2) |
| 81 |
def getattr(self, obj, attr): |
| 82 |
return getattr(self, 'get_' + attr)(obj) |
| 83 |
|
| 84 |
@specialize.arg(0, 2) |
| 85 |
def setattr(self, obj, attr, value): |
| 86 |
return getattr(self, 'set_' + attr)(obj, value) |
| 87 |
|
| 88 |
|
| 89 |
def delegate(boundmethod, *args_s): |
| 90 |
bk = getbookkeeper() |
| 91 |
s_meth = bk.immutablevalue(boundmethod) |
| 92 |
return bk.emulate_pbc_call(bk.position_key, s_meth, args_s, |
| 93 |
callback = bk.position_key) |
| 94 |
|
| 95 |
class BoxEntry(ExtRegistryEntry): |
| 96 |
_about_ = controlled_instance_box |
| 97 |
|
| 98 |
def compute_result_annotation(self, s_controller, s_real_obj): |
| 99 |
if s_real_obj == annmodel.s_ImpossibleValue: |
| 100 |
return annmodel.s_ImpossibleValue |
| 101 |
else: |
| 102 |
assert s_controller.is_constant() |
| 103 |
controller = s_controller.const |
| 104 |
return SomeControlledInstance(s_real_obj, controller=controller) |
| 105 |
|
| 106 |
def specialize_call(self, hop): |
| 107 |
if not isinstance(hop.r_result, ControlledInstanceRepr): |
| 108 |
raise TyperError("box() should return ControlledInstanceRepr,\n" |
| 109 |
"got %r" % (hop.r_result,)) |
| 110 |
hop.exception_cannot_occur() |
| 111 |
return hop.inputarg(hop.r_result.r_real_obj, arg=1) |
| 112 |
|
| 113 |
class UnboxEntry(ExtRegistryEntry): |
| 114 |
_about_ = controlled_instance_unbox |
| 115 |
|
| 116 |
def compute_result_annotation(self, s_controller, s_obj): |
| 117 |
if s_obj == annmodel.s_ImpossibleValue: |
| 118 |
return annmodel.s_ImpossibleValue |
| 119 |
else: |
| 120 |
assert isinstance(s_obj, SomeControlledInstance) |
| 121 |
return s_obj.s_real_obj |
| 122 |
|
| 123 |
def specialize_call(self, hop): |
| 124 |
if not isinstance(hop.args_r[1], ControlledInstanceRepr): |
| 125 |
raise TyperError("unbox() should take a ControlledInstanceRepr,\n" |
| 126 |
"got %r" % (hop.args_r[1],)) |
| 127 |
hop.exception_cannot_occur() |
| 128 |
v = hop.inputarg(hop.args_r[1], arg=1) |
| 129 |
return hop.llops.convertvar(v, hop.args_r[1].r_real_obj, hop.r_result) |
| 130 |
|
| 131 |
class IsBoxEntry(ExtRegistryEntry): |
| 132 |
_about_ = controlled_instance_is_box |
| 133 |
|
| 134 |
def compute_result_annotation(self, s_controller, s_obj): |
| 135 |
if s_obj == annmodel.s_ImpossibleValue: |
| 136 |
return annmodel.s_ImpossibleValue |
| 137 |
else: |
| 138 |
assert s_controller.is_constant() |
| 139 |
controller = s_controller.const |
| 140 |
result = (isinstance(s_obj, SomeControlledInstance) and |
| 141 |
s_obj.controller == controller) |
| 142 |
return self.bookkeeper.immutablevalue(result) |
| 143 |
|
| 144 |
def specialize_call(self, hop): |
| 145 |
from rpython.rtyper.lltypesystem import lltype |
| 146 |
assert hop.s_result.is_constant() |
| 147 |
hop.exception_cannot_occur() |
| 148 |
return hop.inputconst(lltype.Bool, hop.s_result.const) |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
class SomeControlledInstance(annmodel.SomeObject): |
| 153 |
|
| 154 |
def __init__(self, s_real_obj, controller): |
| 155 |
self.s_real_obj = s_real_obj |
| 156 |
self.controller = controller |
| 157 |
self.knowntype = controller.knowntype |
| 158 |
|
| 159 |
def can_be_none(self): |
| 160 |
return self.controller.can_be_None |
| 161 |
|
| 162 |
def noneify(self): |
| 163 |
return SomeControlledInstance(self.s_real_obj, self.controller) |
| 164 |
|
| 165 |
def rtyper_makerepr(self, rtyper): |
| 166 |
return ControlledInstanceRepr(rtyper, self.s_real_obj, self.controller) |
| 167 |
|
| 168 |
def rtyper_makekey(self): |
| 169 |
real_key = self.s_real_obj.rtyper_makekey() |
| 170 |
return self.__class__, real_key, self.controller |
| 171 |
|
| 172 |
def getattr(self, s_attr): |
| 173 |
assert s_attr.is_constant() |
| 174 |
ctrl = self.controller |
| 175 |
return delegate(ctrl.getattr, self.s_real_obj, s_attr) |
| 176 |
|
| 177 |
def setattr(self, s_attr, s_value): |
| 178 |
assert s_attr.is_constant() |
| 179 |
ctrl = self.controller |
| 180 |
return delegate(ctrl.setattr, self.s_real_obj, s_attr, s_value) |
| 181 |
|
| 182 |
def bool(self): |
| 183 |
ctrl = self.controller |
| 184 |
return delegate(ctrl.bool, self.s_real_obj) |
| 185 |
|
| 186 |
def simple_call(self, *args_s): |
| 187 |
return delegate(self.controller.call, self.s_real_obj, *args_s) |
| 188 |
|
| 189 |
|
| 190 |
class __extend__(pairtype(SomeControlledInstance, annmodel.SomeObject)): |
| 191 |
|
| 192 |
def getitem((s_cin, s_key)): |
| 193 |
return delegate(s_cin.controller.getitem, s_cin.s_real_obj, s_key) |
| 194 |
|
| 195 |
def setitem((s_cin, s_key), s_value): |
| 196 |
delegate(s_cin.controller.setitem, s_cin.s_real_obj, s_key, s_value) |
| 197 |
|
| 198 |
def delitem((s_cin, s_key)): |
| 199 |
delegate(s_cin.controller.delitem, s_cin.s_real_obj, s_key) |
| 200 |
|
| 201 |
|
| 202 |
class __extend__(pairtype(SomeControlledInstance, SomeControlledInstance)): |
| 203 |
|
| 204 |
def union((s_cin1, s_cin2)): |
| 205 |
if s_cin1.controller is not s_cin2.controller: |
| 206 |
raise annmodel.UnionError("different controller!") |
| 207 |
return SomeControlledInstance(annmodel.unionof(s_cin1.s_real_obj, |
| 208 |
s_cin2.s_real_obj), |
| 209 |
s_cin1.controller) |
| 210 |
|
| 211 |
class ControlledInstanceRepr(Repr): |
| 212 |
|
| 213 |
def __init__(self, rtyper, s_real_obj, controller): |
| 214 |
self.rtyper = rtyper |
| 215 |
self.s_real_obj = s_real_obj |
| 216 |
self.r_real_obj = rtyper.getrepr(s_real_obj) |
| 217 |
self.controller = controller |
| 218 |
self.lowleveltype = self.r_real_obj.lowleveltype |
| 219 |
|
| 220 |
def convert_const(self, value): |
| 221 |
real_value = self.controller.convert(value) |
| 222 |
return self.r_real_obj.convert_const(real_value) |
| 223 |
|
| 224 |
def reveal(self, r): |
| 225 |
if r is not self: |
| 226 |
raise TyperError("expected %r, got %r" % (self, r)) |
| 227 |
return self.s_real_obj, self.r_real_obj |
| 228 |
|
| 229 |
def rtype_getattr(self, hop): |
| 230 |
return rtypedelegate(self.controller.getattr, hop) |
| 231 |
|
| 232 |
def rtype_setattr(self, hop): |
| 233 |
return rtypedelegate(self.controller.setattr, hop) |
| 234 |
|
| 235 |
def rtype_bool(self, hop): |
| 236 |
return rtypedelegate(self.controller.bool, hop) |
| 237 |
|
| 238 |
def rtype_simple_call(self, hop): |
| 239 |
return rtypedelegate(self.controller.call, hop) |
| 240 |
|
| 241 |
|
| 242 |
class __extend__(pairtype(ControlledInstanceRepr, Repr)): |
| 243 |
|
| 244 |
def rtype_getitem((r_controlled, r_key), hop): |
| 245 |
return rtypedelegate(r_controlled.controller.getitem, hop) |
| 246 |
|
| 247 |
def rtype_setitem((r_controlled, r_key), hop): |
| 248 |
return rtypedelegate(r_controlled.controller.setitem, hop) |
| 249 |
|
| 250 |
def rtype_delitem((r_controlled, r_key), hop): |
| 251 |
return rtypedelegate(r_controlled.controller.delitem, hop) |
| 252 |
|
| 253 |
|
| 254 |
def rtypedelegate(callable, hop, revealargs=[0], revealresult=False): |
| 255 |
bk = hop.rtyper.annotator.bookkeeper |
| 256 |
c_meth = Constant(callable) |
| 257 |
s_meth = bk.immutablevalue(callable) |
| 258 |
hop2 = hop.copy() |
| 259 |
for index in revealargs: |
| 260 |
r_controlled = hop2.args_r[index] |
| 261 |
if not isinstance(r_controlled, ControlledInstanceRepr): |
| 262 |
raise TyperError("args_r[%d] = %r, expected ControlledInstanceRepr" |
| 263 |
% (index, r_controlled)) |
| 264 |
s_new, r_new = r_controlled.s_real_obj, r_controlled.r_real_obj |
| 265 |
hop2.args_s[index], hop2.args_r[index] = s_new, r_new |
| 266 |
v = hop2.args_v[index] |
| 267 |
if isinstance(v, Constant): |
| 268 |
real_value = r_controlled.controller.convert(v.value) |
| 269 |
hop2.args_v[index] = Constant(real_value) |
| 270 |
if revealresult: |
| 271 |
r_controlled = hop2.r_result |
| 272 |
if not isinstance(r_controlled, ControlledInstanceRepr): |
| 273 |
raise TyperError("r_result = %r, expected ControlledInstanceRepr" |
| 274 |
% (r_controlled,)) |
| 275 |
s_new, r_new = r_controlled.s_real_obj, r_controlled.r_real_obj |
| 276 |
hop2.s_result, hop2.r_result = s_new, r_new |
| 277 |
hop2.v_s_insertfirstarg(c_meth, s_meth) |
| 278 |
spaceop = op.simple_call(*hop2.args_v) |
| 279 |
spaceop.result = hop2.spaceop.result |
| 280 |
hop2.spaceop = spaceop |
| 281 |
return hop2.dispatch() |