Skip to content

Commit

Permalink
Use class-based NamedTuples in mypyc (#14442)
Browse files Browse the repository at this point in the history
  • Loading branch information
ichard26 authored Jan 13, 2023
1 parent c4a5f56 commit 3ba22ee
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 36 deletions.
9 changes: 5 additions & 4 deletions mypyc/ir/class_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@
# placed in the class's shadow vtable (if it has one).


VTableMethod = NamedTuple(
"VTableMethod",
[("cls", "ClassIR"), ("name", str), ("method", FuncIR), ("shadow_method", Optional[FuncIR])],
)
class VTableMethod(NamedTuple):
cls: "ClassIR"
name: str
method: FuncIR
shadow_method: Optional[FuncIR]


VTableEntries = List[VTableMethod]
Expand Down
6 changes: 3 additions & 3 deletions mypyc/ir/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,6 @@ def visit_keep_alive(self, op: KeepAlive) -> T:
#
# (Serialization and deserialization *will* be used for incremental
# compilation but so far it is not hooked up to anything.)
DeserMaps = NamedTuple(
"DeserMaps", [("classes", Dict[str, "ClassIR"]), ("functions", Dict[str, "FuncIR"])]
)
class DeserMaps(NamedTuple):
classes: Dict[str, "ClassIR"]
functions: Dict[str, "FuncIR"]
14 changes: 5 additions & 9 deletions mypyc/primitives/int_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,11 @@ def int_unary_op(name: str, c_function_name: str) -> CFunctionDescription:
# c_func_description: the C function to call when operands are tagged integers
# c_func_negated: whether to negate the C function call's result
# c_func_swap_operands: whether to swap lhs and rhs when call the function
IntComparisonOpDescription = NamedTuple(
"IntComparisonOpDescription",
[
("binary_op_variant", int),
("c_func_description", CFunctionDescription),
("c_func_negated", bool),
("c_func_swap_operands", bool),
],
)
class IntComparisonOpDescription(NamedTuple):
binary_op_variant: int
c_func_description: CFunctionDescription
c_func_negated: bool
c_func_swap_operands: bool


# Equals operation on two boxed tagged integers
Expand Down
38 changes: 18 additions & 20 deletions mypyc/primitives/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,27 @@
# is only used for primitives. We translate it away during IR building.
ERR_NEG_INT: Final = 10

CFunctionDescription = NamedTuple(
"CFunctionDescription",
[
("name", str),
("arg_types", List[RType]),
("return_type", RType),
("var_arg_type", Optional[RType]),
("truncated_type", Optional[RType]),
("c_function_name", str),
("error_kind", int),
("steals", StealsDescription),
("is_borrowed", bool),
("ordering", Optional[List[int]]),
("extra_int_constants", List[Tuple[int, RType]]),
("priority", int),
],
)

class CFunctionDescription(NamedTuple):
name: str
arg_types: List[RType]
return_type: RType
var_arg_type: Optional[RType]
truncated_type: Optional[RType]
c_function_name: str
error_kind: int
steals: StealsDescription
is_borrowed: bool
ordering: Optional[List[int]]
extra_int_constants: List[Tuple[int, RType]]
priority: int


# A description for C load operations including LoadGlobal and LoadAddress
LoadAddressDescription = NamedTuple(
"LoadAddressDescription", [("name", str), ("type", RType), ("src", str)]
) # name of the target to load
class LoadAddressDescription(NamedTuple):
name: str
type: RType
src: str # name of the target to load


# CallC op for method call(such as 'str.join')
Expand Down

0 comments on commit 3ba22ee

Please sign in to comment.