| 1 |
"""'Runtime subtype' check for RTypes. |
| 2 |
|
| 3 |
A type S is a runtime subtype of T if a value of type S can be used at runtime |
| 4 |
when a value of type T is expected without requiring any runtime conversions. |
| 5 |
|
| 6 |
For boxed types, runtime subtyping is the same as regular subtyping. |
| 7 |
Unboxed subtypes, on the other hand, are not runtime subtypes of object |
| 8 |
(since they require boxing to be used as an object), but short ints |
| 9 |
are runtime subtypes of int. |
| 10 |
|
| 11 |
Subtyping is used to determine whether an object can be in a |
| 12 |
particular place and runtime subtyping is used to determine whether a |
| 13 |
coercion is necessary first. |
| 14 |
""" |
| 15 |
|
| 16 |
from __future__ import annotations |
| 17 |
|
| 18 |
from mypyc.ir.rtypes import ( |
| 19 |
RArray, |
| 20 |
RInstance, |
| 21 |
RPrimitive, |
| 22 |
RStruct, |
| 23 |
RTuple, |
| 24 |
RType, |
| 25 |
RTypeVisitor, |
| 26 |
RUnion, |
| 27 |
RVec, |
| 28 |
RVoid, |
| 29 |
is_bit_rprimitive, |
| 30 |
is_bool_rprimitive, |
| 31 |
is_int_rprimitive, |
| 32 |
is_short_int_rprimitive, |
| 33 |
) |
| 34 |
from mypyc.subtype import is_subtype |
| 35 |
|
| 36 |
|
| 37 |
def is_runtime_subtype(left: RType, right: RType) -> bool: |
| 38 |
if isinstance(right, RUnion) and not isinstance(left, RUnion): |
| 39 |
return any(not item.is_unboxed and is_runtime_subtype(left, item) for item in right.items) |
| 40 |
return left.accept(RTSubtypeVisitor(right)) |
| 41 |
|
| 42 |
|
| 43 |
class RTSubtypeVisitor(RTypeVisitor[bool]): |
| 44 |
"""Is left a runtime subtype of right? |
| 45 |
|
| 46 |
A few special cases such as right being 'object' are handled in |
| 47 |
is_runtime_subtype and don't need to be covered here. |
| 48 |
""" |
| 49 |
|
| 50 |
def __init__(self, right: RType) -> None: |
| 51 |
self.right = right |
| 52 |
|
| 53 |
def visit_rinstance(self, left: RInstance) -> bool: |
| 54 |
return is_subtype(left, self.right) |
| 55 |
|
| 56 |
def visit_rvec(self, left: RVec) -> bool: |
| 57 |
# TODO: Better implementation |
| 58 |
return left == self.right |
| 59 |
|
| 60 |
def visit_runion(self, left: RUnion) -> bool: |
| 61 |
return not self.right.is_unboxed and is_subtype(left, self.right) |
| 62 |
|
| 63 |
def visit_rprimitive(self, left: RPrimitive) -> bool: |
| 64 |
if is_short_int_rprimitive(left) and is_int_rprimitive(self.right): |
| 65 |
return True |
| 66 |
if is_bit_rprimitive(left) and is_bool_rprimitive(self.right): |
| 67 |
return True |
| 68 |
return left is self.right |
| 69 |
|
| 70 |
def visit_rtuple(self, left: RTuple) -> bool: |
| 71 |
if isinstance(self.right, RTuple): |
| 72 |
return len(self.right.types) == len(left.types) and all( |
| 73 |
is_runtime_subtype(t1, t2) for t1, t2 in zip(left.types, self.right.types) |
| 74 |
) |
| 75 |
return False |
| 76 |
|
| 77 |
def visit_rstruct(self, left: RStruct) -> bool: |
| 78 |
return isinstance(self.right, RStruct) and self.right.name == left.name |
| 79 |
|
| 80 |
def visit_rarray(self, left: RArray) -> bool: |
| 81 |
return left == self.right |
| 82 |
|
| 83 |
def visit_rvoid(self, left: RVoid) -> bool: |
| 84 |
return isinstance(self.right, RVoid) |