| 1 |
"""Subtype check for RTypes.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from mypyc.ir.rtypes import ( |
| 6 |
RArray, |
| 7 |
RInstance, |
| 8 |
RPrimitive, |
| 9 |
RStruct, |
| 10 |
RTuple, |
| 11 |
RType, |
| 12 |
RTypeVisitor, |
| 13 |
RUnion, |
| 14 |
RVec, |
| 15 |
RVoid, |
| 16 |
is_bit_rprimitive, |
| 17 |
is_bool_rprimitive, |
| 18 |
is_fixed_width_rtype, |
| 19 |
is_int_rprimitive, |
| 20 |
is_object_rprimitive, |
| 21 |
is_short_int_rprimitive, |
| 22 |
is_tagged, |
| 23 |
is_tuple_rprimitive, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
def is_subtype(left: RType, right: RType, *, relaxed: bool = False) -> bool: |
| 28 |
if is_object_rprimitive(right): |
| 29 |
return True |
| 30 |
elif isinstance(right, RUnion): |
| 31 |
if isinstance(left, RUnion): |
| 32 |
for left_item in left.items: |
| 33 |
if not any( |
| 34 |
is_subtype(left_item, right_item, relaxed=relaxed) |
| 35 |
for right_item in right.items |
| 36 |
): |
| 37 |
return False |
| 38 |
return True |
| 39 |
else: |
| 40 |
return any(is_subtype(left, item, relaxed=relaxed) for item in right.items) |
| 41 |
return left.accept(SubtypeVisitor(right, relaxed=relaxed)) |
| 42 |
|
| 43 |
|
| 44 |
class SubtypeVisitor(RTypeVisitor[bool]): |
| 45 |
"""Is left a subtype of right? |
| 46 |
|
| 47 |
A few special cases such as right being 'object' are handled in |
| 48 |
is_subtype and don't need to be covered here. |
| 49 |
""" |
| 50 |
|
| 51 |
def __init__(self, right: RType, *, relaxed: bool = False) -> None: |
| 52 |
self.right = right |
| 53 |
self.relaxed = relaxed |
| 54 |
|
| 55 |
def visit_rinstance(self, left: RInstance) -> bool: |
| 56 |
return isinstance(self.right, RInstance) and self.right.class_ir in left.class_ir.mro |
| 57 |
|
| 58 |
def visit_rvec(self, left: RVec) -> bool: |
| 59 |
# TODO: Better implementation |
| 60 |
return left == self.right |
| 61 |
|
| 62 |
def visit_runion(self, left: RUnion) -> bool: |
| 63 |
return all(is_subtype(item, self.right, relaxed=self.relaxed) for item in left.items) |
| 64 |
|
| 65 |
def visit_rprimitive(self, left: RPrimitive) -> bool: |
| 66 |
right = self.right |
| 67 |
if is_bool_rprimitive(left): |
| 68 |
if is_tagged(right) or is_fixed_width_rtype(right): |
| 69 |
return True |
| 70 |
elif is_bit_rprimitive(left): |
| 71 |
if is_bool_rprimitive(right) or is_tagged(right) or is_fixed_width_rtype(right): |
| 72 |
return True |
| 73 |
elif is_short_int_rprimitive(left): |
| 74 |
if is_int_rprimitive(right): |
| 75 |
return True |
| 76 |
if self.relaxed and is_fixed_width_rtype(right): |
| 77 |
return True |
| 78 |
elif is_int_rprimitive(left): |
| 79 |
if self.relaxed and is_fixed_width_rtype(right): |
| 80 |
return True |
| 81 |
elif is_fixed_width_rtype(left): |
| 82 |
if is_int_rprimitive(right): |
| 83 |
return True |
| 84 |
return left is right |
| 85 |
|
| 86 |
def visit_rtuple(self, left: RTuple) -> bool: |
| 87 |
if is_tuple_rprimitive(self.right): |
| 88 |
return True |
| 89 |
if isinstance(self.right, RTuple): |
| 90 |
return len(self.right.types) == len(left.types) and all( |
| 91 |
is_subtype(t1, t2, relaxed=self.relaxed) |
| 92 |
for t1, t2 in zip(left.types, self.right.types) |
| 93 |
) |
| 94 |
return False |
| 95 |
|
| 96 |
def visit_rstruct(self, left: RStruct) -> bool: |
| 97 |
return isinstance(self.right, RStruct) and self.right.name == left.name |
| 98 |
|
| 99 |
def visit_rarray(self, left: RArray) -> bool: |
| 100 |
return left == self.right |
| 101 |
|
| 102 |
def visit_rvoid(self, left: RVoid) -> bool: |
| 103 |
return isinstance(self.right, RVoid) |