| 1 |
"""Primitive tuple ops for *variable-length* tuples. |
| 2 |
|
| 3 |
Note: Varying-length tuples are represented as boxed Python tuple |
| 4 |
objects, i.e. tuple_rprimitive (RPrimitive), not RTuple. |
| 5 |
""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER |
| 10 |
from mypyc.ir.rtypes import ( |
| 11 |
bit_rprimitive, |
| 12 |
c_pyssize_t_rprimitive, |
| 13 |
int_rprimitive, |
| 14 |
list_rprimitive, |
| 15 |
object_rprimitive, |
| 16 |
tuple_rprimitive, |
| 17 |
void_rtype, |
| 18 |
) |
| 19 |
from mypyc.primitives.registry import binary_op, custom_op, function_op, load_address_op, method_op |
| 20 |
|
| 21 |
# Get the 'builtins.tuple' type object. |
| 22 |
load_address_op(name="builtins.tuple", type=object_rprimitive, src="PyTuple_Type") |
| 23 |
|
| 24 |
# tuple[index] (for an int index) |
| 25 |
tuple_get_item_op = method_op( |
| 26 |
name="__getitem__", |
| 27 |
arg_types=[tuple_rprimitive, int_rprimitive], |
| 28 |
return_type=object_rprimitive, |
| 29 |
c_function_name="CPySequenceTuple_GetItem", |
| 30 |
error_kind=ERR_MAGIC, |
| 31 |
) |
| 32 |
|
| 33 |
# This is unsafe because it assumes that the index is a non-negative integer |
| 34 |
# that is in-bounds for the tuple. |
| 35 |
tuple_get_item_unsafe_op = custom_op( |
| 36 |
arg_types=[tuple_rprimitive, c_pyssize_t_rprimitive], |
| 37 |
return_type=object_rprimitive, |
| 38 |
c_function_name="CPySequenceTuple_GetItemUnsafe", |
| 39 |
error_kind=ERR_NEVER, |
| 40 |
) |
| 41 |
|
| 42 |
# Construct a boxed tuple from items: (item1, item2, ...) |
| 43 |
new_tuple_op = custom_op( |
| 44 |
arg_types=[c_pyssize_t_rprimitive], |
| 45 |
return_type=tuple_rprimitive, |
| 46 |
c_function_name="PyTuple_Pack", |
| 47 |
error_kind=ERR_MAGIC, |
| 48 |
var_arg_type=object_rprimitive, |
| 49 |
) |
| 50 |
|
| 51 |
new_tuple_with_length_op = custom_op( |
| 52 |
arg_types=[c_pyssize_t_rprimitive], |
| 53 |
return_type=tuple_rprimitive, |
| 54 |
c_function_name="PyTuple_New", |
| 55 |
error_kind=ERR_MAGIC, |
| 56 |
) |
| 57 |
|
| 58 |
load_empty_tuple_constant_op = custom_op( |
| 59 |
arg_types=[], |
| 60 |
return_type=tuple_rprimitive, |
| 61 |
c_function_name="CPyTuple_LoadEmptyTupleConstant", |
| 62 |
error_kind=ERR_NEVER, |
| 63 |
) |
| 64 |
|
| 65 |
# PyTuple_SET_ITEM does no error checking, |
| 66 |
# and should only be used to fill in brand new tuples. |
| 67 |
new_tuple_set_item_op = custom_op( |
| 68 |
arg_types=[tuple_rprimitive, c_pyssize_t_rprimitive, object_rprimitive], |
| 69 |
return_type=void_rtype, |
| 70 |
c_function_name="CPySequenceTuple_SetItemUnsafe", |
| 71 |
error_kind=ERR_NEVER, |
| 72 |
steals=[False, False, True], |
| 73 |
) |
| 74 |
|
| 75 |
# Construct tuple from a list. |
| 76 |
list_tuple_op = function_op( |
| 77 |
name="builtins.tuple", |
| 78 |
arg_types=[list_rprimitive], |
| 79 |
return_type=tuple_rprimitive, |
| 80 |
c_function_name="PyList_AsTuple", |
| 81 |
error_kind=ERR_MAGIC, |
| 82 |
priority=2, |
| 83 |
) |
| 84 |
|
| 85 |
# Construct tuple from an arbitrary (iterable) object. |
| 86 |
sequence_tuple_op = function_op( |
| 87 |
name="builtins.tuple", |
| 88 |
arg_types=[object_rprimitive], |
| 89 |
return_type=tuple_rprimitive, |
| 90 |
c_function_name="PySequence_Tuple", |
| 91 |
error_kind=ERR_MAGIC, |
| 92 |
) |
| 93 |
|
| 94 |
# translate isinstance(obj, tuple) |
| 95 |
isinstance_tuple = function_op( |
| 96 |
name="builtins.isinstance", |
| 97 |
arg_types=[object_rprimitive], |
| 98 |
return_type=bit_rprimitive, |
| 99 |
c_function_name="PyTuple_Check", |
| 100 |
error_kind=ERR_NEVER, |
| 101 |
) |
| 102 |
|
| 103 |
# tuple + tuple |
| 104 |
binary_op( |
| 105 |
name="+", |
| 106 |
arg_types=[tuple_rprimitive, tuple_rprimitive], |
| 107 |
return_type=tuple_rprimitive, |
| 108 |
c_function_name="PySequence_Concat", |
| 109 |
error_kind=ERR_MAGIC, |
| 110 |
) |
| 111 |
|
| 112 |
# tuple * int |
| 113 |
binary_op( |
| 114 |
name="*", |
| 115 |
arg_types=[tuple_rprimitive, int_rprimitive], |
| 116 |
return_type=tuple_rprimitive, |
| 117 |
c_function_name="CPySequence_Multiply", |
| 118 |
error_kind=ERR_MAGIC, |
| 119 |
) |
| 120 |
|
| 121 |
# int * tuple |
| 122 |
binary_op( |
| 123 |
name="*", |
| 124 |
arg_types=[int_rprimitive, tuple_rprimitive], |
| 125 |
return_type=tuple_rprimitive, |
| 126 |
c_function_name="CPySequence_RMultiply", |
| 127 |
error_kind=ERR_MAGIC, |
| 128 |
) |
| 129 |
|
| 130 |
# tuple[begin:end] |
| 131 |
tuple_slice_op = custom_op( |
| 132 |
arg_types=[tuple_rprimitive, int_rprimitive, int_rprimitive], |
| 133 |
return_type=object_rprimitive, |
| 134 |
c_function_name="CPySequenceTuple_GetSlice", |
| 135 |
error_kind=ERR_MAGIC, |
| 136 |
) |