Generate specialized slice op for some index expressions. Return None if a specialized op isn't available. This supports obj[x:y], obj[:x], and obj[x:] for a few types.
(builder: IRBuilder, base: Value, index: SliceExpr)
| 801 | |
| 802 | |
| 803 | def try_gen_slice_op(builder: IRBuilder, base: Value, index: SliceExpr) -> Value | None: |
| 804 | """Generate specialized slice op for some index expressions. |
| 805 | |
| 806 | Return None if a specialized op isn't available. |
| 807 | |
| 808 | This supports obj[x:y], obj[:x], and obj[x:] for a few types. |
| 809 | """ |
| 810 | if index.stride: |
| 811 | # We can only handle the default stride of 1. |
| 812 | return None |
| 813 | |
| 814 | if index.begin_index: |
| 815 | begin_type = builder.node_type(index.begin_index) |
| 816 | else: |
| 817 | begin_type = int_rprimitive |
| 818 | if index.end_index: |
| 819 | end_type = builder.node_type(index.end_index) |
| 820 | else: |
| 821 | end_type = int_rprimitive |
| 822 | |
| 823 | # Both begin and end index must be int (or missing). |
| 824 | if is_any_int(begin_type) and is_any_int(end_type): |
| 825 | if index.begin_index: |
| 826 | begin = builder.accept(index.begin_index) |
| 827 | else: |
| 828 | begin = builder.load_int(0) |
| 829 | if index.end_index: |
| 830 | end = builder.accept(index.end_index) |
| 831 | else: |
| 832 | # Replace missing end index with the largest short integer |
| 833 | # (a sequence can't be longer). |
| 834 | end = builder.load_int(MAX_SHORT_INT) |
| 835 | if isinstance(base.type, RVec): |
| 836 | return vec_slice(builder.builder, base, begin, end, index.line) |
| 837 | candidates = [list_slice_op, tuple_slice_op, str_slice_op, bytes_slice_op] |
| 838 | return builder.builder.matching_call_c(candidates, [base, begin, end], index.line) |
| 839 | |
| 840 | return None |
| 841 | |
| 842 | |
| 843 | def transform_conditional_expr(builder: IRBuilder, expr: ConditionalExpr) -> Value: |
no test coverage detected
searching dependent graphs…