MCPcopy Index your code
hub / github.com/python/mypy / RStruct

Class RStruct

mypyc/ir/rtypes.py:894–952  ·  view source on GitHub ↗

C struct type

Source from the content-addressed store, hash-verified

892
893@final
894class RStruct(RType):
895 """C struct type"""
896
897 def __init__(self, name: str, names: list[str], types: list[RType]) -> None:
898 self.name = name
899 self.names = names
900 self.types = types
901 self.is_refcounted = any(t.is_refcounted for t in self.types)
902
903 # generate dummy names
904 if len(self.names) < len(self.types):
905 for i in range(len(self.types) - len(self.names)):
906 self.names.append("_item" + str(i))
907 self.offsets, self.size = compute_aligned_offsets_and_size(types)
908 self._ctype = name
909
910 def field_type(self, name: str) -> RType:
911 for n, t in zip(self.names, self.types):
912 if n == name:
913 return t
914 assert False, f"{self.name} has no field '{name}'"
915
916 def accept(self, visitor: RTypeVisitor[T]) -> T:
917 return visitor.visit_rstruct(self)
918
919 @property
920 def may_be_immortal(self) -> bool:
921 return False
922
923 def __str__(self) -> str:
924 # if not tuple(unnamed structs)
925 return "{}{{{}}}".format(
926 self.name,
927 ", ".join(name + ":" + str(typ) for name, typ in zip(self.names, self.types)),
928 )
929
930 def __repr__(self) -> str:
931 return "<RStruct {}{{{}}}>".format(
932 self.name,
933 ", ".join(name + ":" + repr(typ) for name, typ in zip(self.names, self.types)),
934 )
935
936 def __eq__(self, other: object) -> TypeGuard[RStruct]:
937 return (
938 isinstance(other, RStruct)
939 and self.name == other.name
940 and self.names == other.names
941 and self.types == other.types
942 )
943
944 def __hash__(self) -> int:
945 return hash((self.name, tuple(self.names), tuple(self.types)))
946
947 def serialize(self) -> JsonDict:
948 assert False
949
950 @classmethod
951 def deserialize(cls, data: JsonDict, ctx: DeserMaps) -> RStruct:

Callers 7

test_struct_offsetsMethod · 0.90
test_struct_strMethod · 0.90
test_runtime_subtypeMethod · 0.90
test_eq_and_hashMethod · 0.90
setUpMethod · 0.90
test_get_element_ptrMethod · 0.90
rtypes.pyFile · 0.85

Calls

no outgoing calls

Tested by 6

test_struct_offsetsMethod · 0.72
test_struct_strMethod · 0.72
test_runtime_subtypeMethod · 0.72
test_eq_and_hashMethod · 0.72
setUpMethod · 0.72
test_get_element_ptrMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…