| 1 |
from __future__ import absolute_import |
| 2 |
|
| 3 |
import types |
| 4 |
from collections import defaultdict |
| 5 |
from contextlib import contextmanager |
| 6 |
|
| 7 |
from rpython.tool.ansi_print import AnsiLogger |
| 8 |
from rpython.tool.pairtype import pair |
| 9 |
from rpython.tool.error import (format_blocked_annotation_error, |
| 10 |
gather_error, source_lines) |
| 11 |
from rpython.flowspace.model import Variable, Constant, checkgraph |
| 12 |
from rpython.translator import simplify, transform |
| 13 |
from rpython.annotator import model as annmodel, signature |
| 14 |
from rpython.annotator.model import ( |
| 15 |
typeof, s_ImpossibleValue, SomeInstance, intersection, difference) |
| 16 |
from rpython.annotator.bookkeeper import Bookkeeper |
| 17 |
from rpython.rtyper.normalizecalls import perform_normalizations |
| 18 |
|
| 19 |
log = AnsiLogger("annrpython") |
| 20 |
|
| 21 |
|
| 22 |
class RPythonAnnotator(object): |
| 23 |
"""Block annotator for RPython. |
| 24 |
See description in doc/translation.txt.""" |
| 25 |
|
| 26 |
def __init__(self, translator=None, policy=None, bookkeeper=None, |
| 27 |
keepgoing=False): |
| 28 |
import rpython.rtyper.extfuncregistry |
| 29 |
|
| 30 |
if translator is None: |
| 31 |
|
| 32 |
from rpython.translator.translator import TranslationContext |
| 33 |
translator = TranslationContext() |
| 34 |
translator.annotator = self |
| 35 |
self.translator = translator |
| 36 |
self.genpendingblocks=[{}] |
| 37 |
self.annotated = {} |
| 38 |
self.added_blocks = None |
| 39 |
self.links_followed = {} |
| 40 |
self.notify = {} |
| 41 |
self.fixed_graphs = {} |
| 42 |
self.blocked_blocks = {} |
| 43 |
|
| 44 |
self.blocked_graphs = {} |
| 45 |
|
| 46 |
self.frozen = False |
| 47 |
if policy is None: |
| 48 |
from rpython.annotator.policy import AnnotatorPolicy |
| 49 |
self.policy = AnnotatorPolicy() |
| 50 |
else: |
| 51 |
self.policy = policy |
| 52 |
if bookkeeper is None: |
| 53 |
bookkeeper = Bookkeeper(self) |
| 54 |
self.bookkeeper = bookkeeper |
| 55 |
self.keepgoing = keepgoing |
| 56 |
self.failed_blocks = set() |
| 57 |
self.errors = [] |
| 58 |
|
| 59 |
def __getstate__(self): |
| 60 |
attrs = """translator genpendingblocks annotated links_followed |
| 61 |
notify bookkeeper frozen policy added_blocks""".split() |
| 62 |
ret = self.__dict__.copy() |
| 63 |
for key, value in ret.items(): |
| 64 |
if key not in attrs: |
| 65 |
assert type(value) is dict, ( |
| 66 |
"%r is not dict. please update %s.__getstate__" % |
| 67 |
(key, self.__class__.__name__)) |
| 68 |
ret[key] = {} |
| 69 |
return ret |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
def build_types(self, function, input_arg_types, complete_now=True, |
| 74 |
main_entry_point=False): |
| 75 |
"""Recursively build annotations about the specific entry point.""" |
| 76 |
assert isinstance(function, types.FunctionType), "fix that!" |
| 77 |
|
| 78 |
from rpython.annotator.policy import AnnotatorPolicy |
| 79 |
policy = AnnotatorPolicy() |
| 80 |
|
| 81 |
args_s = [self.typeannotation(t) for t in input_arg_types] |
| 82 |
|
| 83 |
|
| 84 |
annmodel.TLS.check_str_without_nul = ( |
| 85 |
self.translator.config.translation.check_str_without_nul) |
| 86 |
|
| 87 |
with self.using_policy(policy): |
| 88 |
flowgraph, inputs_s = self.get_call_parameters(function, args_s) |
| 89 |
|
| 90 |
if main_entry_point: |
| 91 |
self.translator.entry_point_graph = flowgraph |
| 92 |
return self.build_graph_types(flowgraph, inputs_s, complete_now=complete_now) |
| 93 |
|
| 94 |
def get_call_parameters(self, function, args_s): |
| 95 |
with self.bookkeeper.at_position(None): |
| 96 |
desc = self.bookkeeper.getdesc(function) |
| 97 |
return desc.get_call_parameters(args_s) |
| 98 |
|
| 99 |
def annotate_helper(self, function, args_s, policy=None): |
| 100 |
if policy is None: |
| 101 |
from rpython.annotator.policy import AnnotatorPolicy |
| 102 |
policy = AnnotatorPolicy() |
| 103 |
|
| 104 |
annmodel.TLS.check_str_without_nul = ( |
| 105 |
self.translator.config.translation.check_str_without_nul) |
| 106 |
with self.using_policy(policy): |
| 107 |
graph, inputcells = self.get_call_parameters(function, args_s) |
| 108 |
self.build_graph_types(graph, inputcells, complete_now=False) |
| 109 |
self.complete_helpers() |
| 110 |
return graph |
| 111 |
|
| 112 |
def complete_helpers(self): |
| 113 |
saved = self.added_blocks |
| 114 |
self.added_blocks = {} |
| 115 |
try: |
| 116 |
self.complete() |
| 117 |
|
| 118 |
self.simplify(block_subset=self.added_blocks) |
| 119 |
finally: |
| 120 |
self.added_blocks = saved |
| 121 |
|
| 122 |
@contextmanager |
| 123 |
def using_policy(self, policy): |
| 124 |
"""A context manager that temporarily replaces the annotator policy""" |
| 125 |
old_policy = self.policy |
| 126 |
self.policy = policy |
| 127 |
yield |
| 128 |
self.policy = old_policy |
| 129 |
|
| 130 |
def build_graph_types(self, flowgraph, inputcells, complete_now=True): |
| 131 |
checkgraph(flowgraph) |
| 132 |
|
| 133 |
nbarg = len(flowgraph.getargs()) |
| 134 |
assert len(inputcells) == nbarg |
| 135 |
|
| 136 |
|
| 137 |
self.addpendinggraph(flowgraph, inputcells) |
| 138 |
|
| 139 |
if complete_now: |
| 140 |
self.complete() |
| 141 |
return self.annotation(flowgraph.getreturnvar()) |
| 142 |
|
| 143 |
def gettype(self, variable): |
| 144 |
"""Return the known type of a control flow graph variable, |
| 145 |
defaulting to 'object'.""" |
| 146 |
if isinstance(variable, Constant): |
| 147 |
return type(variable.value) |
| 148 |
elif isinstance(variable, Variable): |
| 149 |
s_variable = variable.annotation |
| 150 |
if s_variable: |
| 151 |
return s_variable.knowntype |
| 152 |
else: |
| 153 |
return object |
| 154 |
else: |
| 155 |
raise TypeError("Variable or Constant instance expected, " |
| 156 |
"got %r" % (variable,)) |
| 157 |
|
| 158 |
def getuserclassdefinitions(self): |
| 159 |
"""Return a list of ClassDefs.""" |
| 160 |
return self.bookkeeper.classdefs |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
def addpendinggraph(self, flowgraph, inputcells): |
| 165 |
self.addpendingblock(flowgraph, flowgraph.startblock, inputcells) |
| 166 |
|
| 167 |
def addpendingblock(self, graph, block, cells): |
| 168 |
"""Register an entry point into block with the given input cells.""" |
| 169 |
if graph in self.fixed_graphs: |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
for a, s_newarg in zip(block.inputargs, cells): |
| 175 |
s_oldarg = a.annotation |
| 176 |
|
| 177 |
|
| 178 |
if annmodel.unionof(s_oldarg, s_newarg) != s_oldarg: |
| 179 |
raise annmodel.AnnotatorError( |
| 180 |
"Late-stage annotation is not allowed to modify the " |
| 181 |
"existing annotation for variable %s: %s" % |
| 182 |
(a, s_oldarg)) |
| 183 |
|
| 184 |
else: |
| 185 |
assert not self.frozen |
| 186 |
if block not in self.annotated: |
| 187 |
self.bindinputargs(graph, block, cells) |
| 188 |
else: |
| 189 |
self.mergeinputargs(graph, block, cells) |
| 190 |
if not self.annotated[block]: |
| 191 |
self.schedulependingblock(graph, block) |
| 192 |
|
| 193 |
def schedulependingblock(self, graph, block): |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
generation = getattr(block, 'generation', 0) |
| 201 |
self.genpendingblocks[generation][block] = graph |
| 202 |
|
| 203 |
def complete_pending_blocks(self): |
| 204 |
while True: |
| 205 |
|
| 206 |
|
| 207 |
gen = 0 |
| 208 |
for pendingblocks in self.genpendingblocks: |
| 209 |
if pendingblocks: |
| 210 |
break |
| 211 |
gen += 1 |
| 212 |
else: |
| 213 |
return |
| 214 |
|
| 215 |
gen += 1 |
| 216 |
if len(self.genpendingblocks) == gen: |
| 217 |
self.genpendingblocks.append({}) |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
while pendingblocks: |
| 222 |
block, graph = pendingblocks.popitem() |
| 223 |
block.generation = gen |
| 224 |
self.processblock(graph, block) |
| 225 |
|
| 226 |
def complete(self): |
| 227 |
"""Process pending blocks until none is left.""" |
| 228 |
while True: |
| 229 |
self.complete_pending_blocks() |
| 230 |
self.policy.no_more_blocks_to_annotate(self) |
| 231 |
if not any(self.genpendingblocks): |
| 232 |
break |
| 233 |
|
| 234 |
if self.added_blocks is not None: |
| 235 |
newgraphs = [self.annotated[block] for block in self.added_blocks] |
| 236 |
newgraphs = dict.fromkeys(newgraphs) |
| 237 |
got_blocked_blocks = False in newgraphs |
| 238 |
else: |
| 239 |
newgraphs = self.translator.graphs |
| 240 |
got_blocked_blocks = False in self.annotated.values() |
| 241 |
if self.failed_blocks: |
| 242 |
text = ('Annotation failed, %s errors were recorded:' % |
| 243 |
len(self.errors)) |
| 244 |
text += '\n-----'.join(str(e) for e in self.errors) |
| 245 |
raise annmodel.AnnotatorError(text) |
| 246 |
|
| 247 |
if got_blocked_blocks: |
| 248 |
for graph in self.blocked_graphs.values(): |
| 249 |
self.blocked_graphs[graph] = True |
| 250 |
|
| 251 |
blocked_blocks = [block for block, done in self.annotated.items() |
| 252 |
if done is False] |
| 253 |
assert len(blocked_blocks) == len(self.blocked_blocks) |
| 254 |
|
| 255 |
text = format_blocked_annotation_error(self, self.blocked_blocks) |
| 256 |
|
| 257 |
raise annmodel.AnnotatorError(text) |
| 258 |
for graph in newgraphs: |
| 259 |
v = graph.getreturnvar() |
| 260 |
if v.annotation is None: |
| 261 |
self.setbinding(v, s_ImpossibleValue) |
| 262 |
v = graph.exceptblock.inputargs[1] |
| 263 |
if v.annotation is not None and v.annotation.can_be_none(): |
| 264 |
raise annmodel.AnnotatorError( |
| 265 |
"%r is found by annotation to possibly raise None, " |
| 266 |
"but the None was not suppressed by the flow space" % |
| 267 |
(graph,)) |
| 268 |
|
| 269 |
def validate(self): |
| 270 |
"""Check that the annotation results are valid""" |
| 271 |
self.bookkeeper.check_no_flags_on_instances() |
| 272 |
|
| 273 |
def annotation(self, arg): |
| 274 |
"Gives the SomeValue corresponding to the given Variable or Constant." |
| 275 |
if isinstance(arg, Variable): |
| 276 |
return arg.annotation |
| 277 |
elif isinstance(arg, Constant): |
| 278 |
return self.bookkeeper.immutablevalue(arg.value) |
| 279 |
else: |
| 280 |
raise TypeError('Variable or Constant expected, got %r' % (arg,)) |
| 281 |
|
| 282 |
def binding(self, arg): |
| 283 |
"Gives the SomeValue corresponding to the given Variable or Constant." |
| 284 |
s_arg = self.annotation(arg) |
| 285 |
if s_arg is None: |
| 286 |
raise KeyError |
| 287 |
return s_arg |
| 288 |
|
| 289 |
def typeannotation(self, t): |
| 290 |
return signature.annotation(t, self.bookkeeper) |
| 291 |
|
| 292 |
def setbinding(self, arg, s_value): |
| 293 |
s_old = arg.annotation |
| 294 |
if s_old is not None: |
| 295 |
if not s_value.contains(s_old): |
| 296 |
log.WARNING("%s does not contain %s" % (s_value, s_old)) |
| 297 |
log.WARNING("%s" % annmodel.union(s_value, s_old)) |
| 298 |
assert False |
| 299 |
arg.annotation = s_value |
| 300 |
|
| 301 |
def warning(self, msg, pos=None): |
| 302 |
if pos is None: |
| 303 |
try: |
| 304 |
pos = self.bookkeeper.position_key |
| 305 |
except AttributeError: |
| 306 |
pos = '?' |
| 307 |
if pos != '?': |
| 308 |
pos = self.whereami(pos) |
| 309 |
|
| 310 |
log.WARNING("%s/ %s" % (pos, msg)) |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
def recursivecall(self, graph, whence, inputcells): |
| 316 |
if whence is not None: |
| 317 |
parent_graph, parent_block, parent_index = whence |
| 318 |
tag = parent_block, parent_index |
| 319 |
self.translator.update_call_graph(parent_graph, graph, tag) |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
returnpositions = self.notify.setdefault(graph.returnblock, set()) |
| 324 |
returnpositions.add(whence) |
| 325 |
|
| 326 |
|
| 327 |
self.addpendingblock(graph, graph.startblock, inputcells) |
| 328 |
|
| 329 |
|
| 330 |
v = graph.getreturnvar() |
| 331 |
try: |
| 332 |
return self.binding(v) |
| 333 |
except KeyError: |
| 334 |
|
| 335 |
|
| 336 |
return s_ImpossibleValue |
| 337 |
|
| 338 |
def reflowfromposition(self, position_key): |
| 339 |
graph, block, index = position_key |
| 340 |
self.reflowpendingblock(graph, block) |
| 341 |
|
| 342 |
def call_sites(self): |
| 343 |
newblocks = self.added_blocks |
| 344 |
if newblocks is None: |
| 345 |
newblocks = self.annotated |
| 346 |
for block in newblocks: |
| 347 |
for op in block.operations: |
| 348 |
if op.opname in ('simple_call', 'call_args'): |
| 349 |
yield op |
| 350 |
|
| 351 |
|
| 352 |
if op.result.annotation is None: |
| 353 |
break |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
def simplify(self, block_subset=None, extra_passes=None): |
| 358 |
|
| 359 |
transform.transform_graph(self, block_subset=block_subset, |
| 360 |
extra_passes=extra_passes) |
| 361 |
if block_subset is None: |
| 362 |
graphs = self.translator.graphs |
| 363 |
else: |
| 364 |
graphs = {} |
| 365 |
for block in block_subset: |
| 366 |
graph = self.annotated.get(block) |
| 367 |
if graph: |
| 368 |
graphs[graph] = True |
| 369 |
for graph in graphs: |
| 370 |
simplify.eliminate_empty_blocks(graph) |
| 371 |
self.bookkeeper.compute_at_fixpoint() |
| 372 |
if block_subset is None: |
| 373 |
perform_normalizations(self) |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
def processblock(self, graph, block): |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
self.annotated[block] = graph |
| 393 |
if block in self.failed_blocks: |
| 394 |
return |
| 395 |
if block in self.blocked_blocks: |
| 396 |
del self.blocked_blocks[block] |
| 397 |
try: |
| 398 |
self.flowin(graph, block) |
| 399 |
except BlockedInference as e: |
| 400 |
self.annotated[block] = False |
| 401 |
self.blocked_blocks[block] = (graph, e.opindex) |
| 402 |
except Exception as e: |
| 403 |
|
| 404 |
if not hasattr(e, '__annotator_block'): |
| 405 |
setattr(e, '__annotator_block', block) |
| 406 |
raise |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
if self.added_blocks is not None: |
| 412 |
self.added_blocks[block] = True |
| 413 |
|
| 414 |
def reflowpendingblock(self, graph, block): |
| 415 |
assert not self.frozen |
| 416 |
assert graph not in self.fixed_graphs |
| 417 |
self.schedulependingblock(graph, block) |
| 418 |
assert block in self.annotated |
| 419 |
self.annotated[block] = False |
| 420 |
self.blocked_blocks[block] = (graph, None) |
| 421 |
|
| 422 |
def bindinputargs(self, graph, block, inputcells): |
| 423 |
|
| 424 |
assert len(block.inputargs) == len(inputcells) |
| 425 |
for a, cell in zip(block.inputargs, inputcells): |
| 426 |
self.setbinding(a, cell) |
| 427 |
self.annotated[block] = False |
| 428 |
self.blocked_blocks[block] = (graph, None) |
| 429 |
|
| 430 |
def mergeinputargs(self, graph, block, inputcells): |
| 431 |
|
| 432 |
|
| 433 |
oldcells = [self.binding(a) for a in block.inputargs] |
| 434 |
try: |
| 435 |
unions = [annmodel.unionof(c1,c2) for c1, c2 in zip(oldcells,inputcells)] |
| 436 |
except annmodel.UnionError as e: |
| 437 |
|
| 438 |
e.source = '\n'.join(source_lines(graph, block, None, long=True)) |
| 439 |
if self.keepgoing: |
| 440 |
self.errors.append(e) |
| 441 |
self.failed_blocks.add(block) |
| 442 |
return |
| 443 |
raise |
| 444 |
|
| 445 |
if unions != oldcells: |
| 446 |
self.bindinputargs(graph, block, unions) |
| 447 |
|
| 448 |
def apply_renaming(self, s_out, renaming): |
| 449 |
if hasattr(s_out, 'is_type_of'): |
| 450 |
renamed_is_type_of = [] |
| 451 |
for v in s_out.is_type_of: |
| 452 |
renamed_is_type_of += renaming[v] |
| 453 |
assert s_out.knowntype is type |
| 454 |
newcell = typeof(renamed_is_type_of) |
| 455 |
if s_out.is_constant(): |
| 456 |
newcell.const = s_out.const |
| 457 |
s_out = newcell |
| 458 |
|
| 459 |
if hasattr(s_out, 'knowntypedata'): |
| 460 |
renamed_knowntypedata = {} |
| 461 |
for value, constraints in s_out.knowntypedata.items(): |
| 462 |
renamed_knowntypedata[value] = {} |
| 463 |
for v, s in constraints.items(): |
| 464 |
new_vs = renaming.get(v, []) |
| 465 |
for new_v in new_vs: |
| 466 |
renamed_knowntypedata[value][new_v] = s |
| 467 |
assert isinstance(s_out, annmodel.SomeBool) |
| 468 |
newcell = annmodel.SomeBool() |
| 469 |
if s_out.is_constant(): |
| 470 |
newcell.const = s_out.const |
| 471 |
s_out = newcell |
| 472 |
s_out.set_knowntypedata(renamed_knowntypedata) |
| 473 |
return s_out |
| 474 |
|
| 475 |
|
| 476 |
def whereami(self, position_key): |
| 477 |
graph, block, i = position_key |
| 478 |
blk = "" |
| 479 |
if block: |
| 480 |
at = block.at() |
| 481 |
if at: |
| 482 |
blk = " block"+at |
| 483 |
opid="" |
| 484 |
if i is not None: |
| 485 |
opid = " op=%d" % i |
| 486 |
return repr(graph) + blk + opid |
| 487 |
|
| 488 |
def flowin(self, graph, block): |
| 489 |
try: |
| 490 |
i = 0 |
| 491 |
while i < len(block.operations): |
| 492 |
op = block.operations[i] |
| 493 |
with self.bookkeeper.at_position((graph, block, i)): |
| 494 |
new_ops = op.transform(self) |
| 495 |
if new_ops is not None: |
| 496 |
block.operations[i:i+1] = new_ops |
| 497 |
if not new_ops: |
| 498 |
continue |
| 499 |
new_ops[-1].result = op.result |
| 500 |
op = new_ops[0] |
| 501 |
self.consider_op(op) |
| 502 |
i += 1 |
| 503 |
|
| 504 |
except BlockedInference as e: |
| 505 |
if e.op is block.raising_op: |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
exits = [link for link in block.exits |
| 511 |
if link.exitcase is not None] |
| 512 |
|
| 513 |
elif e.op.opname in ('simple_call', 'call_args', 'next'): |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
return |
| 520 |
|
| 521 |
else: |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
e.opindex = i |
| 526 |
raise |
| 527 |
|
| 528 |
except annmodel.HarmlesslyBlocked: |
| 529 |
return |
| 530 |
|
| 531 |
except annmodel.AnnotatorError as e: |
| 532 |
e.source = gather_error(self, graph, block, i) |
| 533 |
if self.keepgoing: |
| 534 |
self.errors.append(e) |
| 535 |
self.failed_blocks.add(block) |
| 536 |
return |
| 537 |
raise |
| 538 |
|
| 539 |
else: |
| 540 |
|
| 541 |
|
| 542 |
exits = block.exits |
| 543 |
if isinstance(block.exitswitch, Variable): |
| 544 |
s_exitswitch = self.binding(block.exitswitch) |
| 545 |
if s_exitswitch.is_constant(): |
| 546 |
exits = [link for link in exits |
| 547 |
if link.exitcase == s_exitswitch.const] |
| 548 |
|
| 549 |
if block.canraise: |
| 550 |
op = block.raising_op |
| 551 |
s_exception = self.get_exception(op) |
| 552 |
for link in exits: |
| 553 |
case = link.exitcase |
| 554 |
if case is None: |
| 555 |
self.follow_link(graph, link, {}) |
| 556 |
continue |
| 557 |
if s_exception == s_ImpossibleValue: |
| 558 |
break |
| 559 |
s_case = SomeInstance(self.bookkeeper.getuniqueclassdef(case)) |
| 560 |
s_matching_exc = intersection(s_exception, s_case) |
| 561 |
if s_matching_exc != s_ImpossibleValue: |
| 562 |
self.follow_raise_link(graph, link, s_matching_exc) |
| 563 |
s_exception = difference(s_exception, s_case) |
| 564 |
else: |
| 565 |
if isinstance(block.exitswitch, Variable): |
| 566 |
knowntypedata = getattr( |
| 567 |
block.exitswitch.annotation, "knowntypedata", {}) |
| 568 |
else: |
| 569 |
knowntypedata = {} |
| 570 |
for link in exits: |
| 571 |
constraints = knowntypedata.get(link.exitcase, {}) |
| 572 |
self.follow_link(graph, link, constraints) |
| 573 |
|
| 574 |
if block in self.notify: |
| 575 |
for position in self.notify[block]: |
| 576 |
self.reflowfromposition(position) |
| 577 |
|
| 578 |
|
| 579 |
def follow_link(self, graph, link, constraints): |
| 580 |
assert not (isinstance(link.exitcase, (types.ClassType, type)) and |
| 581 |
issubclass(link.exitcase, BaseException)) |
| 582 |
|
| 583 |
ignore_link = False |
| 584 |
inputs_s = [] |
| 585 |
renaming = defaultdict(list) |
| 586 |
for v_out, v_input in zip(link.args, link.target.inputargs): |
| 587 |
renaming[v_out].append(v_input) |
| 588 |
|
| 589 |
for v_out in link.args: |
| 590 |
s_out = self.annotation(v_out) |
| 591 |
if v_out in constraints: |
| 592 |
s_constraint = constraints[v_out] |
| 593 |
s_out = pair(s_out, s_constraint).improve() |
| 594 |
|
| 595 |
if s_out == s_ImpossibleValue: |
| 596 |
ignore_link = True |
| 597 |
s_out = self.apply_renaming(s_out, renaming) |
| 598 |
inputs_s.append(s_out) |
| 599 |
if ignore_link: |
| 600 |
return |
| 601 |
|
| 602 |
self.links_followed[link] = True |
| 603 |
self.addpendingblock(graph, link.target, inputs_s) |
| 604 |
|
| 605 |
def follow_raise_link(self, graph, link, s_last_exc_value): |
| 606 |
v_last_exc_type = link.last_exception |
| 607 |
v_last_exc_value = link.last_exc_value |
| 608 |
|
| 609 |
assert (isinstance(link.exitcase, (types.ClassType, type)) and |
| 610 |
issubclass(link.exitcase, BaseException)) |
| 611 |
|
| 612 |
assert v_last_exc_type and v_last_exc_value |
| 613 |
|
| 614 |
if isinstance(v_last_exc_value, Variable): |
| 615 |
self.setbinding(v_last_exc_value, s_last_exc_value) |
| 616 |
|
| 617 |
if isinstance(v_last_exc_type, Variable): |
| 618 |
self.setbinding(v_last_exc_type, typeof([v_last_exc_value])) |
| 619 |
|
| 620 |
inputs_s = [] |
| 621 |
renaming = defaultdict(list) |
| 622 |
for v_out, v_input in zip(link.args, link.target.inputargs): |
| 623 |
renaming[v_out].append(v_input) |
| 624 |
|
| 625 |
for v_out, v_input in zip(link.args, link.target.inputargs): |
| 626 |
if v_out == v_last_exc_type: |
| 627 |
s_out = typeof(renaming[v_last_exc_value]) |
| 628 |
if isinstance(v_last_exc_type, Constant): |
| 629 |
s_out.const = v_last_exc_type.value |
| 630 |
elif v_last_exc_type.annotation.is_constant(): |
| 631 |
s_out.const = v_last_exc_type.annotation.const |
| 632 |
inputs_s.append(s_out) |
| 633 |
else: |
| 634 |
s_out = self.annotation(v_out) |
| 635 |
s_out = self.apply_renaming(s_out, renaming) |
| 636 |
inputs_s.append(s_out) |
| 637 |
|
| 638 |
self.links_followed[link] = True |
| 639 |
self.addpendingblock(graph, link.target, inputs_s) |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
def consider_op(self, op): |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
for arg in op.args: |
| 651 |
if isinstance(self.annotation(arg), annmodel.SomeImpossibleValue): |
| 652 |
raise BlockedInference(self, op, -1) |
| 653 |
resultcell = op.consider(self) |
| 654 |
if resultcell is None: |
| 655 |
resultcell = s_ImpossibleValue |
| 656 |
elif resultcell == s_ImpossibleValue: |
| 657 |
raise BlockedInference(self, op, -1) |
| 658 |
assert isinstance(resultcell, annmodel.SomeObject) |
| 659 |
assert isinstance(op.result, Variable) |
| 660 |
self.setbinding(op.result, resultcell) |
| 661 |
|
| 662 |
def get_exception(self, operation): |
| 663 |
""" |
| 664 |
Return the annotation for all exceptions that `operation` may raise. |
| 665 |
""" |
| 666 |
can_only_throw = operation.get_can_only_throw(self) |
| 667 |
if can_only_throw is None: |
| 668 |
return SomeInstance(self.bookkeeper.getuniqueclassdef(Exception)) |
| 669 |
else: |
| 670 |
return self.bookkeeper.new_exception(can_only_throw) |
| 671 |
|
| 672 |
|
| 673 |
class BlockedInference(Exception): |
| 674 |
"""This exception signals the type inference engine that the situation |
| 675 |
is currently blocked, and that it should try to progress elsewhere.""" |
| 676 |
|
| 677 |
def __init__(self, annotator, op, opindex): |
| 678 |
self.annotator = annotator |
| 679 |
try: |
| 680 |
self.break_at = annotator.bookkeeper.position_key |
| 681 |
except AttributeError: |
| 682 |
self.break_at = None |
| 683 |
self.op = op |
| 684 |
self.opindex = opindex |
| 685 |
|
| 686 |
def __repr__(self): |
| 687 |
if not self.break_at: |
| 688 |
break_at = "?" |
| 689 |
else: |
| 690 |
break_at = self.annotator.whereami(self.break_at) |
| 691 |
return "<BlockedInference break_at %s [%s]>" %(break_at, self.op) |
| 692 |
|
| 693 |
__str__ = __repr__ |