|
| 1 | +from dataclasses import field, dataclass |
| 2 | + |
| 3 | +from kirin.ir import types |
| 4 | + |
| 5 | + |
| 6 | +@dataclass |
| 7 | +class TypeResolutionResult: |
| 8 | + pass |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class ResolutionOk(TypeResolutionResult): |
| 13 | + |
| 14 | + def __bool__(self): |
| 15 | + return True |
| 16 | + |
| 17 | + |
| 18 | +Ok = ResolutionOk() |
| 19 | + |
| 20 | + |
| 21 | +@dataclass |
| 22 | +class ResolutionError(TypeResolutionResult): |
| 23 | + expr: types.TypeAttribute |
| 24 | + value: types.TypeAttribute |
| 25 | + |
| 26 | + def __bool__(self): |
| 27 | + return False |
| 28 | + |
| 29 | + def __str__(self): |
| 30 | + return f"expected {self.expr}, got {self.value}" |
| 31 | + |
| 32 | + |
| 33 | +@dataclass |
| 34 | +class TypeResolution: |
| 35 | + vars: dict[types.TypeVar, types.TypeAttribute] = field(default_factory=dict) |
| 36 | + |
| 37 | + def substitute(self, typ: types.TypeAttribute) -> types.TypeAttribute: |
| 38 | + if isinstance(typ, types.TypeVar): |
| 39 | + return self.vars.get(typ, typ) |
| 40 | + elif isinstance(typ, types.Generic): |
| 41 | + return types.Generic( |
| 42 | + typ.body, *tuple(self.substitute(var) for var in typ.vars) |
| 43 | + ) |
| 44 | + elif isinstance(typ, types.Union): |
| 45 | + return types.Union(self.substitute(t) for t in typ.types) |
| 46 | + return typ |
| 47 | + |
| 48 | + def solve( |
| 49 | + self, annot: types.TypeAttribute, value: types.TypeAttribute |
| 50 | + ) -> TypeResolutionResult: |
| 51 | + if isinstance(annot, types.TypeVar): |
| 52 | + return self.solve_TypeVar(annot, value) |
| 53 | + elif isinstance(annot, types.Generic): |
| 54 | + return self.solve_Generic(annot, value) |
| 55 | + elif isinstance(annot, types.Union): |
| 56 | + return self.solve_Union(annot, value) |
| 57 | + |
| 58 | + if annot.is_subseteq(value): |
| 59 | + return Ok |
| 60 | + else: |
| 61 | + return ResolutionError(annot, value) |
| 62 | + |
| 63 | + def solve_TypeVar(self, annot: types.TypeVar, value: types.TypeAttribute): |
| 64 | + if annot in self.vars: |
| 65 | + if value.is_subseteq(self.vars[annot]): |
| 66 | + self.vars[annot] = value |
| 67 | + elif self.vars[annot].is_subseteq(value): |
| 68 | + pass |
| 69 | + else: |
| 70 | + return ResolutionError(annot, value) |
| 71 | + else: |
| 72 | + self.vars[annot] = value |
| 73 | + return Ok |
| 74 | + |
| 75 | + def solve_Generic(self, annot: types.Generic, value: types.TypeAttribute): |
| 76 | + if not isinstance(value, types.Generic): |
| 77 | + return ResolutionError(annot, value) |
| 78 | + |
| 79 | + if not value.body.is_subseteq(annot.body): |
| 80 | + return ResolutionError(annot.body, value.body) |
| 81 | + |
| 82 | + for var, val in zip(annot.vars, value.vars): |
| 83 | + result = self.solve(var, val) |
| 84 | + if not result: |
| 85 | + return result |
| 86 | + |
| 87 | + if not annot.vararg: |
| 88 | + return Ok |
| 89 | + |
| 90 | + for val in value.vars[len(annot.vars) :]: |
| 91 | + result = self.solve(annot.vararg.typ, val) |
| 92 | + if not result: |
| 93 | + return result |
| 94 | + return Ok |
| 95 | + |
| 96 | + def solve_Union(self, annot: types.Union, value: types.TypeAttribute): |
| 97 | + for typ in annot.types: |
| 98 | + result = self.solve(typ, value) |
| 99 | + if result: |
| 100 | + return Ok |
| 101 | + return ResolutionError(annot, value) |
0 commit comments