|
83 | 83 | ) |
84 | 84 | from mypyc.irbuild.ast_helpers import is_borrow_friendly_expr, process_conditional |
85 | 85 | from mypyc.irbuild.builder import IRBuilder, int_borrow_friendly_op |
86 | | -from mypyc.irbuild.constant_fold import constant_fold_expr |
| 86 | +from mypyc.irbuild.constant_fold import constant_fold_expr, folding_candidate, try_constant_fold |
87 | 87 | from mypyc.irbuild.for_helpers import ( |
88 | 88 | comprehension_helper, |
89 | 89 | raise_error_if_contains_unreachable_names, |
|
113 | 113 | from mypyc.primitives.str_ops import str_slice_op |
114 | 114 | from mypyc.primitives.tuple_ops import list_tuple_op, tuple_slice_op |
115 | 115 |
|
116 | | -TransformFunc = Callable[[IRBuilder, Expression], Value | None] |
117 | | - |
118 | | - |
119 | 116 | # Name and attribute references |
120 | 117 |
|
121 | 118 |
|
@@ -605,30 +602,6 @@ def transform_index_expr(builder: IRBuilder, expr: IndexExpr) -> Value: |
605 | 602 | ) |
606 | 603 |
|
607 | 604 |
|
608 | | -def try_constant_fold(builder: IRBuilder, expr: Expression) -> Value | None: |
609 | | - """Return the constant value of an expression if possible. |
610 | | -
|
611 | | - Return None otherwise. |
612 | | - """ |
613 | | - value = constant_fold_expr(builder, expr) |
614 | | - if value is not None: |
615 | | - return builder.load_literal_value(value) |
616 | | - return None |
617 | | - |
618 | | - |
619 | | -def folding_candidate(transform: TransformFunc) -> TransformFunc: |
620 | | - """Mark a transform function as a candidate for constant folding. |
621 | | -
|
622 | | - Candidate functions will attempt to short-circuit the transformation |
623 | | - by constant folding the expression and will only proceed to transform |
624 | | - the expression if folding is not possible. |
625 | | - """ |
626 | | - def constant_fold_wrap(builder: IRBuilder, expr: Expression) -> Value | None: |
627 | | - folded = try_constant_fold(builder, expr) |
628 | | - return folded if folded is not None else transform(builder, expr) |
629 | | - return constant_fold_wrap |
630 | | - |
631 | | - |
632 | 605 | def try_gen_slice_op(builder: IRBuilder, base: Value, index: SliceExpr) -> Value | None: |
633 | 606 | """Generate specialized slice op for some index expressions. |
634 | 607 |
|
|
0 commit comments