Skip to content

Commit d864ad4

Browse files
committed
Add VTable
1 parent 00d66eb commit d864ad4

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

kmir/src/kmir/smir.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ def from_dict(dct: dict[str, Any]) -> GlobalAlloc:
404404
match dct:
405405
case {'Function': _}:
406406
return Function.from_dict(dct)
407+
case {'VTable': _}:
408+
return VTable.from_dict(dct)
407409
case {'Static': _}:
408410
return Static.from_dict(dct)
409411
case {'Memory': _}:
@@ -477,6 +479,154 @@ def from_dict(obj: Any) -> Virtual:
477479
class Shim(InstanceKind): ...
478480

479481

482+
@dataclass
483+
class VTable(GlobalAlloc):
484+
ty: Ty
485+
binder: ExistentialTraitRefBinder | None
486+
487+
@staticmethod
488+
def from_dict(dct: dict[str, Any]) -> VTable:
489+
return VTable(
490+
ty=Ty(dct['VTable'][0]),
491+
binder=ExistentialTraitRefBinder.from_dict(dct['VTable'][1]) if dct['VTable'][1] is not None else None,
492+
)
493+
494+
495+
@dataclass
496+
class ExistentialTraitRefBinder:
497+
value: ExistentialTraitRef
498+
bound_vars: list[BoundVariableKind]
499+
500+
@staticmethod
501+
def from_dict(dct: dict[str, Any]) -> ExistentialTraitRefBinder:
502+
return ExistentialTraitRefBinder(
503+
value=ExistentialTraitRef.from_dict(dct['value']),
504+
bound_vars=[BoundVariableKind.from_dict(var) for var in dct['bound_vars']],
505+
)
506+
507+
508+
@dataclass
509+
class ExistentialTraitRef:
510+
def_id: DefId
511+
generic_args: list[GenericArgKind]
512+
513+
@staticmethod
514+
def from_dict(dct: dict[str, Any]) -> ExistentialTraitRef:
515+
return ExistentialTraitRef(
516+
def_id=DefId(dct['def_id']),
517+
generic_args=[GenericArgKind.from_dict(arg) for arg in dct['generic_args']],
518+
)
519+
520+
521+
@dataclass
522+
class BoundVariableKind(ABC): # noqa: B024
523+
@staticmethod
524+
def from_dict(dct: Any) -> BoundVariableKind:
525+
match dct:
526+
case {'Ty': _}:
527+
return BVTy.from_dict(dct)
528+
case {'Region': _}:
529+
return BVRegion.from_dict(dct)
530+
case 'Const':
531+
return BVConst()
532+
case _:
533+
raise ValueError(f'Invalid BoundBariableKind data: {dct}')
534+
535+
536+
@dataclass
537+
class BVTy(BoundVariableKind):
538+
kind: BoundTyKind
539+
540+
@staticmethod
541+
def from_dict(dct: Any) -> BVTy:
542+
return BVTy(kind=BoundTyKind.from_dict(dct['Ty']))
543+
544+
545+
class BoundTyKind(ABC): # noqa: B024
546+
@staticmethod
547+
def from_dict(dct: Any) -> BoundTyKind:
548+
match dct:
549+
case 'Anon':
550+
return BTAnon()
551+
case {'Param': _}:
552+
return BTParam.from_dict(dct)
553+
case _:
554+
raise ValueError(f'Invalid BoundTyKind data: {dct}')
555+
556+
557+
@dataclass
558+
class BTAnon(BoundTyKind): ...
559+
560+
561+
@dataclass
562+
class BTParam(BoundTyKind):
563+
def_id: DefId
564+
name: str
565+
566+
@staticmethod
567+
def from_dict(dct: Any) -> BTParam:
568+
return BTParam(
569+
def_id=DefId(dct['Param'][0]),
570+
name=str(dct['Param'][1]),
571+
)
572+
573+
574+
@dataclass
575+
class BVRegion(BoundVariableKind):
576+
kind: BoundRegionKind
577+
578+
@staticmethod
579+
def from_dict(dct: Any) -> BVRegion:
580+
return BVRegion(kind=BoundRegionKind.from_dict(dct['Region']))
581+
582+
583+
class BoundRegionKind(ABC): # noqa: B024
584+
@staticmethod
585+
def from_dict(dct: Any) -> BoundRegionKind:
586+
match dct:
587+
case 'BrAnon':
588+
return BRAnon()
589+
case {'BrNamed': _}:
590+
return BRNamed.from_dict(dct)
591+
case 'BrEnv':
592+
return BREnv()
593+
case _:
594+
raise ValueError(f'Invalid BoundRegionKind data: {dct}')
595+
596+
597+
@dataclass
598+
class BRAnon(BoundRegionKind): ...
599+
600+
601+
@dataclass
602+
class BRNamed(BoundRegionKind):
603+
def_id: DefId
604+
name: str
605+
606+
@staticmethod
607+
def from_dict(dct: Any) -> BRNamed:
608+
return BRNamed(
609+
def_id=DefId(dct['BrNamed'][0]),
610+
name=str(dct['BrNamed'][1]),
611+
)
612+
613+
614+
@dataclass
615+
class BREnv(BoundRegionKind): ...
616+
617+
618+
@dataclass
619+
class BVConst(BoundVariableKind): ...
620+
621+
622+
@dataclass
623+
class GenericArgKind:
624+
@staticmethod
625+
def from_dict(dct: dict[str, Any]) -> GenericArgKind:
626+
_LOGGER.warning(f'Unparsed GenericArgKind data encountered: {dct}')
627+
return GenericArgKind()
628+
629+
480630
@dataclass
481631
class Static(GlobalAlloc):
482632
def_id: DefId

0 commit comments

Comments
 (0)