| 1 |
from mypyc.ir.deps import LIBRT_VECS, VECS_EXTRA_OPS |
| 2 |
from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER |
| 3 |
from mypyc.ir.rtypes import ( |
| 4 |
RTypeVar, |
| 5 |
RVec, |
| 6 |
bit_rprimitive, |
| 7 |
bytes_rprimitive, |
| 8 |
int64_rprimitive, |
| 9 |
object_rprimitive, |
| 10 |
uint8_rprimitive, |
| 11 |
) |
| 12 |
from mypyc.primitives.registry import custom_primitive_op, function_op |
| 13 |
|
| 14 |
# isinstance(obj, vec) |
| 15 |
isinstance_vec = function_op( |
| 16 |
name="builtins.isinstance", |
| 17 |
arg_types=[object_rprimitive], |
| 18 |
return_type=bit_rprimitive, |
| 19 |
c_function_name="CPyVec_Check", |
| 20 |
error_kind=ERR_NEVER, |
| 21 |
dependencies=[LIBRT_VECS, VECS_EXTRA_OPS], |
| 22 |
) |
| 23 |
|
| 24 |
# bytes(vec[u8]) |
| 25 |
function_op( |
| 26 |
name="builtins.bytes", |
| 27 |
arg_types=[RVec(uint8_rprimitive)], |
| 28 |
return_type=bytes_rprimitive, |
| 29 |
c_function_name="CPyVecU8_ToBytes", |
| 30 |
error_kind=ERR_MAGIC, |
| 31 |
dependencies=[LIBRT_VECS, VECS_EXTRA_OPS], |
| 32 |
) |
| 33 |
|
| 34 |
# Get vec item, assuming the index is valid (no bounds check) |
| 35 |
vec_get_item_unsafe_op = custom_primitive_op( |
| 36 |
name="vec_get_item_unsafe", |
| 37 |
arg_types=[RVec(RTypeVar(0)), int64_rprimitive], |
| 38 |
return_type=RTypeVar(0), |
| 39 |
error_kind=ERR_NEVER, |
| 40 |
type_params=[RTypeVar(0)], |
| 41 |
) |
| 42 |
|
| 43 |
# Like vec_get_item_unsafe, but the result is a borrowed reference |
| 44 |
vec_get_item_unsafe_borrow_op = custom_primitive_op( |
| 45 |
name="vec_get_item_unsafe_borrow", |
| 46 |
arg_types=[RVec(RTypeVar(0)), int64_rprimitive], |
| 47 |
is_borrowed=True, |
| 48 |
return_type=RTypeVar(0), |
| 49 |
error_kind=ERR_NEVER, |
| 50 |
type_params=[RTypeVar(0)], |
| 51 |
) |