|
17 | 17 |
|
18 | 18 | from mypy.nodes import (
|
19 | 19 | ArgKind,
|
20 |
| - AwaitExpr, |
21 | 20 | ClassDef,
|
22 | 21 | Decorator,
|
23 | 22 | FuncDef,
|
|
27 | 26 | SymbolNode,
|
28 | 27 | TypeInfo,
|
29 | 28 | Var,
|
30 |
| - YieldExpr, |
31 |
| - YieldFromExpr, |
32 | 29 | )
|
33 | 30 | from mypy.types import CallableType, get_proper_type
|
34 | 31 | from mypyc.common import LAMBDA_NAME, SELF_NAME
|
|
44 | 41 | )
|
45 | 42 | from mypyc.ir.ops import (
|
46 | 43 | BasicBlock,
|
47 |
| - Branch, |
48 | 44 | GetAttr,
|
49 | 45 | InitStatic,
|
50 | 46 | Integer,
|
|
62 | 58 | bool_rprimitive,
|
63 | 59 | dict_rprimitive,
|
64 | 60 | int_rprimitive,
|
65 |
| - object_pointer_rprimitive, |
66 | 61 | object_rprimitive,
|
67 | 62 | )
|
68 | 63 | from mypyc.irbuild.builder import IRBuilder, SymbolTarget, gen_arg_defaults
|
|
88 | 83 | populate_switch_for_generator_class,
|
89 | 84 | setup_env_for_generator_class,
|
90 | 85 | )
|
91 |
| -from mypyc.irbuild.statement import transform_try_except |
92 | 86 | from mypyc.irbuild.targets import AssignmentTarget
|
93 | 87 | from mypyc.irbuild.util import is_constant
|
94 | 88 | from mypyc.primitives.dict_ops import dict_get_method_with_none, dict_new_op, dict_set_item_op
|
95 |
| -from mypyc.primitives.generic_ops import iter_op, next_raw_op, py_setattr_op |
96 |
| -from mypyc.primitives.misc_ops import ( |
97 |
| - check_stop_op, |
98 |
| - coro_op, |
99 |
| - register_function, |
100 |
| - send_op, |
101 |
| - yield_from_except_op, |
102 |
| -) |
| 89 | +from mypyc.primitives.generic_ops import py_setattr_op |
| 90 | +from mypyc.primitives.misc_ops import register_function |
103 | 91 | from mypyc.primitives.registry import builtin_names
|
104 | 92 | from mypyc.sametype import is_same_method_signature
|
105 | 93 |
|
@@ -178,25 +166,6 @@ def transform_lambda_expr(builder: IRBuilder, expr: LambdaExpr) -> Value:
|
178 | 166 | return func_reg
|
179 | 167 |
|
180 | 168 |
|
181 |
| -def transform_yield_expr(builder: IRBuilder, expr: YieldExpr) -> Value: |
182 |
| - if builder.fn_info.is_coroutine: |
183 |
| - builder.error("async generators are unimplemented", expr.line) |
184 |
| - |
185 |
| - if expr.expr: |
186 |
| - retval = builder.accept(expr.expr) |
187 |
| - else: |
188 |
| - retval = builder.builder.none() |
189 |
| - return emit_yield(builder, retval, expr.line) |
190 |
| - |
191 |
| - |
192 |
| -def transform_yield_from_expr(builder: IRBuilder, o: YieldFromExpr) -> Value: |
193 |
| - return handle_yield_from_and_await(builder, o) |
194 |
| - |
195 |
| - |
196 |
| -def transform_await_expr(builder: IRBuilder, o: AwaitExpr) -> Value: |
197 |
| - return handle_yield_from_and_await(builder, o) |
198 |
| - |
199 |
| - |
200 | 169 | # Internal functions
|
201 | 170 |
|
202 | 171 |
|
@@ -551,112 +520,6 @@ def gen_func_ns(builder: IRBuilder) -> str:
|
551 | 520 | )
|
552 | 521 |
|
553 | 522 |
|
554 |
| -def emit_yield(builder: IRBuilder, val: Value, line: int) -> Value: |
555 |
| - retval = builder.coerce(val, builder.ret_types[-1], line) |
556 |
| - |
557 |
| - cls = builder.fn_info.generator_class |
558 |
| - # Create a new block for the instructions immediately following the yield expression, and |
559 |
| - # set the next label so that the next time '__next__' is called on the generator object, |
560 |
| - # the function continues at the new block. |
561 |
| - next_block = BasicBlock() |
562 |
| - next_label = len(cls.continuation_blocks) |
563 |
| - cls.continuation_blocks.append(next_block) |
564 |
| - builder.assign(cls.next_label_target, Integer(next_label), line) |
565 |
| - builder.add(Return(retval)) |
566 |
| - builder.activate_block(next_block) |
567 |
| - |
568 |
| - add_raise_exception_blocks_to_generator_class(builder, line) |
569 |
| - |
570 |
| - assert cls.send_arg_reg is not None |
571 |
| - return cls.send_arg_reg |
572 |
| - |
573 |
| - |
574 |
| -def handle_yield_from_and_await(builder: IRBuilder, o: Union[YieldFromExpr, AwaitExpr]) -> Value: |
575 |
| - # This is basically an implementation of the code in PEP 380. |
576 |
| - |
577 |
| - # TODO: do we want to use the right types here? |
578 |
| - result = Register(object_rprimitive) |
579 |
| - to_yield_reg = Register(object_rprimitive) |
580 |
| - received_reg = Register(object_rprimitive) |
581 |
| - |
582 |
| - if isinstance(o, YieldFromExpr): |
583 |
| - iter_val = builder.call_c(iter_op, [builder.accept(o.expr)], o.line) |
584 |
| - else: |
585 |
| - iter_val = builder.call_c(coro_op, [builder.accept(o.expr)], o.line) |
586 |
| - |
587 |
| - iter_reg = builder.maybe_spill_assignable(iter_val) |
588 |
| - |
589 |
| - stop_block, main_block, done_block = BasicBlock(), BasicBlock(), BasicBlock() |
590 |
| - _y_init = builder.call_c(next_raw_op, [builder.read(iter_reg)], o.line) |
591 |
| - builder.add(Branch(_y_init, stop_block, main_block, Branch.IS_ERROR)) |
592 |
| - |
593 |
| - # Try extracting a return value from a StopIteration and return it. |
594 |
| - # If it wasn't, this reraises the exception. |
595 |
| - builder.activate_block(stop_block) |
596 |
| - builder.assign(result, builder.call_c(check_stop_op, [], o.line), o.line) |
597 |
| - builder.goto(done_block) |
598 |
| - |
599 |
| - builder.activate_block(main_block) |
600 |
| - builder.assign(to_yield_reg, _y_init, o.line) |
601 |
| - |
602 |
| - # OK Now the main loop! |
603 |
| - loop_block = BasicBlock() |
604 |
| - builder.goto_and_activate(loop_block) |
605 |
| - |
606 |
| - def try_body() -> None: |
607 |
| - builder.assign( |
608 |
| - received_reg, emit_yield(builder, builder.read(to_yield_reg), o.line), o.line |
609 |
| - ) |
610 |
| - |
611 |
| - def except_body() -> None: |
612 |
| - # The body of the except is all implemented in a C function to |
613 |
| - # reduce how much code we need to generate. It returns a value |
614 |
| - # indicating whether to break or yield (or raise an exception). |
615 |
| - val = Register(object_rprimitive) |
616 |
| - val_address = builder.add(LoadAddress(object_pointer_rprimitive, val)) |
617 |
| - to_stop = builder.call_c( |
618 |
| - yield_from_except_op, [builder.read(iter_reg), val_address], o.line |
619 |
| - ) |
620 |
| - |
621 |
| - ok, stop = BasicBlock(), BasicBlock() |
622 |
| - builder.add(Branch(to_stop, stop, ok, Branch.BOOL)) |
623 |
| - |
624 |
| - # The exception got swallowed. Continue, yielding the returned value |
625 |
| - builder.activate_block(ok) |
626 |
| - builder.assign(to_yield_reg, val, o.line) |
627 |
| - builder.nonlocal_control[-1].gen_continue(builder, o.line) |
628 |
| - |
629 |
| - # The exception was a StopIteration. Stop iterating. |
630 |
| - builder.activate_block(stop) |
631 |
| - builder.assign(result, val, o.line) |
632 |
| - builder.nonlocal_control[-1].gen_break(builder, o.line) |
633 |
| - |
634 |
| - def else_body() -> None: |
635 |
| - # Do a next() or a .send(). It will return NULL on exception |
636 |
| - # but it won't automatically propagate. |
637 |
| - _y = builder.call_c(send_op, [builder.read(iter_reg), builder.read(received_reg)], o.line) |
638 |
| - ok, stop = BasicBlock(), BasicBlock() |
639 |
| - builder.add(Branch(_y, stop, ok, Branch.IS_ERROR)) |
640 |
| - |
641 |
| - # Everything's fine. Yield it. |
642 |
| - builder.activate_block(ok) |
643 |
| - builder.assign(to_yield_reg, _y, o.line) |
644 |
| - builder.nonlocal_control[-1].gen_continue(builder, o.line) |
645 |
| - |
646 |
| - # Try extracting a return value from a StopIteration and return it. |
647 |
| - # If it wasn't, this rereaises the exception. |
648 |
| - builder.activate_block(stop) |
649 |
| - builder.assign(result, builder.call_c(check_stop_op, [], o.line), o.line) |
650 |
| - builder.nonlocal_control[-1].gen_break(builder, o.line) |
651 |
| - |
652 |
| - builder.push_loop_stack(loop_block, done_block) |
653 |
| - transform_try_except(builder, try_body, [(None, None, except_body)], else_body, o.line) |
654 |
| - builder.pop_loop_stack() |
655 |
| - |
656 |
| - builder.goto_and_activate(done_block) |
657 |
| - return builder.read(result) |
658 |
| - |
659 |
| - |
660 | 523 | def load_decorated_func(builder: IRBuilder, fdef: FuncDef, orig_func_reg: Value) -> Value:
|
661 | 524 | """Apply decorators to a function.
|
662 | 525 |
|
|
0 commit comments