Skip to content

Commit a1cc9c3

Browse files
committed
Use @Final for Value and RType subclasses that shouldn't be subclassed
1 parent 656dc21 commit a1cc9c3

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

mypyc/ir/ops.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class to enable the new behavior. Sometimes adding a new abstract
2727

2828
from abc import abstractmethod
2929
from collections.abc import Sequence
30-
from typing import TYPE_CHECKING, Final, Generic, NamedTuple, TypeVar, Union
30+
from typing import TYPE_CHECKING, Final, Generic, NamedTuple, TypeVar, Union, final
3131

3232
from mypy_extensions import trait
3333

@@ -61,6 +61,7 @@ class to enable the new behavior. Sometimes adding a new abstract
6161
T = TypeVar("T")
6262

6363

64+
@final
6465
class BasicBlock:
6566
"""IR basic block.
6667
@@ -156,6 +157,7 @@ def is_void(self) -> bool:
156157
return isinstance(self.type, RVoid)
157158

158159

160+
@final
159161
class Register(Value):
160162
"""A Register holds a value of a specific type, and it can be read and mutated.
161163
@@ -182,6 +184,7 @@ def __repr__(self) -> str:
182184
return f"<Register {self.name!r} at {hex(id(self))}>"
183185

184186

187+
@final
185188
class Integer(Value):
186189
"""Short integer literal.
187190
@@ -212,6 +215,7 @@ def numeric_value(self) -> int:
212215
return self.value
213216

214217

218+
@final
215219
class Float(Value):
216220
"""Float literal.
217221
@@ -278,6 +282,7 @@ def __init__(self, dest: Register, line: int = -1) -> None:
278282
self.dest = dest
279283

280284

285+
@final
281286
class Assign(BaseAssign):
282287
"""Assign a value to a Register (dest = src)."""
283288

@@ -300,6 +305,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
300305
return visitor.visit_assign(self)
301306

302307

308+
@final
303309
class AssignMulti(BaseAssign):
304310
"""Assign multiple values to a Register (dest = src1, src2, ...).
305311
@@ -345,6 +351,7 @@ def set_target(self, i: int, new: BasicBlock) -> None:
345351
raise AssertionError(f"Invalid set_target({self}, {i})")
346352

347353

354+
@final
348355
class Goto(ControlOp):
349356
"""Unconditional jump."""
350357

@@ -374,6 +381,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
374381
return visitor.visit_goto(self)
375382

376383

384+
@final
377385
class Branch(ControlOp):
378386
"""Branch based on a value.
379387
@@ -440,6 +448,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
440448
return visitor.visit_branch(self)
441449

442450

451+
@final
443452
class Return(ControlOp):
444453
"""Return a value from a function."""
445454

@@ -469,6 +478,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
469478
return visitor.visit_return(self)
470479

471480

481+
@final
472482
class Unreachable(ControlOp):
473483
"""Mark the end of basic block as unreachable.
474484
@@ -525,6 +535,7 @@ def can_raise(self) -> bool:
525535
return self.error_kind != ERR_NEVER
526536

527537

538+
@final
528539
class IncRef(RegisterOp):
529540
"""Increase reference count (inc_ref src)."""
530541

@@ -545,6 +556,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
545556
return visitor.visit_inc_ref(self)
546557

547558

559+
@final
548560
class DecRef(RegisterOp):
549561
"""Decrease reference count and free object if zero (dec_ref src).
550562
@@ -573,6 +585,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
573585
return visitor.visit_dec_ref(self)
574586

575587

588+
@final
576589
class Call(RegisterOp):
577590
"""Native call f(arg, ...).
578591
@@ -601,6 +614,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
601614
return visitor.visit_call(self)
602615

603616

617+
@final
604618
class MethodCall(RegisterOp):
605619
"""Native method call obj.method(arg, ...)"""
606620

@@ -632,6 +646,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
632646
return visitor.visit_method_call(self)
633647

634648

649+
@final
635650
class PrimitiveDescription:
636651
"""Description of a primitive op.
637652
@@ -684,6 +699,7 @@ def __repr__(self) -> str:
684699
return f"<PrimitiveDescription {self.name!r}: {self.arg_types}>"
685700

686701

702+
@final
687703
class PrimitiveOp(RegisterOp):
688704
"""A higher-level primitive operation.
689705
@@ -721,6 +737,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
721737
return visitor.visit_primitive_op(self)
722738

723739

740+
@final
724741
class LoadErrorValue(RegisterOp):
725742
"""Load an error value.
726743
@@ -751,6 +768,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
751768
return visitor.visit_load_error_value(self)
752769

753770

771+
@final
754772
class LoadLiteral(RegisterOp):
755773
"""Load a Python literal object (dest = 'foo' / b'foo' / ...).
756774
@@ -786,6 +804,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
786804
return visitor.visit_load_literal(self)
787805

788806

807+
@final
789808
class GetAttr(RegisterOp):
790809
"""obj.attr (for a native object)"""
791810

@@ -813,6 +832,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
813832
return visitor.visit_get_attr(self)
814833

815834

835+
@final
816836
class SetAttr(RegisterOp):
817837
"""obj.attr = src (for a native object)
818838
@@ -864,6 +884,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
864884
NAMESPACE_TYPE_VAR: Final = "typevar"
865885

866886

887+
@final
867888
class LoadStatic(RegisterOp):
868889
"""Load a static name (name :: static).
869890
@@ -904,6 +925,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
904925
return visitor.visit_load_static(self)
905926

906927

928+
@final
907929
class InitStatic(RegisterOp):
908930
"""static = value :: static
909931
@@ -936,6 +958,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
936958
return visitor.visit_init_static(self)
937959

938960

961+
@final
939962
class TupleSet(RegisterOp):
940963
"""dest = (reg, ...) (for fixed-length tuple)"""
941964

@@ -968,6 +991,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
968991
return visitor.visit_tuple_set(self)
969992

970993

994+
@final
971995
class TupleGet(RegisterOp):
972996
"""Get item of a fixed-length tuple (src[index])."""
973997

@@ -992,6 +1016,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
9921016
return visitor.visit_tuple_get(self)
9931017

9941018

1019+
@final
9951020
class Cast(RegisterOp):
9961021
"""cast(type, src)
9971022
@@ -1023,6 +1048,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
10231048
return visitor.visit_cast(self)
10241049

10251050

1051+
@final
10261052
class Box(RegisterOp):
10271053
"""box(type, src)
10281054
@@ -1057,6 +1083,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
10571083
return visitor.visit_box(self)
10581084

10591085

1086+
@final
10601087
class Unbox(RegisterOp):
10611088
"""unbox(type, src)
10621089
@@ -1083,6 +1110,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
10831110
return visitor.visit_unbox(self)
10841111

10851112

1113+
@final
10861114
class RaiseStandardError(RegisterOp):
10871115
"""Raise built-in exception with an optional error string.
10881116
@@ -1122,6 +1150,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
11221150
StealsDescription = Union[bool, list[bool]]
11231151

11241152

1153+
@final
11251154
class CallC(RegisterOp):
11261155
"""result = function(arg0, arg1, ...)
11271156
@@ -1176,6 +1205,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
11761205
return visitor.visit_call_c(self)
11771206

11781207

1208+
@final
11791209
class Truncate(RegisterOp):
11801210
"""result = truncate src from src_type to dst_type
11811211
@@ -1206,6 +1236,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
12061236
return visitor.visit_truncate(self)
12071237

12081238

1239+
@final
12091240
class Extend(RegisterOp):
12101241
"""result = extend src from src_type to dst_type
12111242
@@ -1240,6 +1271,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
12401271
return visitor.visit_extend(self)
12411272

12421273

1274+
@final
12431275
class LoadGlobal(RegisterOp):
12441276
"""Load a low-level global variable/pointer.
12451277
@@ -1267,6 +1299,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
12671299
return visitor.visit_load_global(self)
12681300

12691301

1302+
@final
12701303
class IntOp(RegisterOp):
12711304
"""Binary arithmetic or bitwise op on integer operands (e.g., r1 = r2 + r3).
12721305
@@ -1331,6 +1364,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
13311364
int_op_to_id: Final = {op: op_id for op_id, op in IntOp.op_str.items()}
13321365

13331366

1367+
@final
13341368
class ComparisonOp(RegisterOp):
13351369
"""Low-level comparison op for integers and pointers.
13361370
@@ -1392,6 +1426,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
13921426
return visitor.visit_comparison_op(self)
13931427

13941428

1429+
@final
13951430
class FloatOp(RegisterOp):
13961431
"""Binary float arithmetic op (e.g., r1 = r2 + r3).
13971432
@@ -1433,6 +1468,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
14331468
float_op_to_id: Final = {op: op_id for op_id, op in FloatOp.op_str.items()}
14341469

14351470

1471+
@final
14361472
class FloatNeg(RegisterOp):
14371473
"""Float negation op (r1 = -r2)."""
14381474

@@ -1453,6 +1489,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
14531489
return visitor.visit_float_neg(self)
14541490

14551491

1492+
@final
14561493
class FloatComparisonOp(RegisterOp):
14571494
"""Low-level comparison op for floats."""
14581495

@@ -1489,6 +1526,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
14891526
float_comparison_op_to_id: Final = {op: op_id for op_id, op in FloatComparisonOp.op_str.items()}
14901527

14911528

1529+
@final
14921530
class LoadMem(RegisterOp):
14931531
"""Read a memory location: result = *(type *)src.
14941532
@@ -1518,6 +1556,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
15181556
return visitor.visit_load_mem(self)
15191557

15201558

1559+
@final
15211560
class SetMem(Op):
15221561
"""Write to a memory location: *(type *)dest = src
15231562
@@ -1549,6 +1588,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
15491588
return visitor.visit_set_mem(self)
15501589

15511590

1591+
@final
15521592
class GetElementPtr(RegisterOp):
15531593
"""Get the address of a struct element.
15541594
@@ -1575,6 +1615,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
15751615
return visitor.visit_get_element_ptr(self)
15761616

15771617

1618+
@final
15781619
class LoadAddress(RegisterOp):
15791620
"""Get the address of a value: result = (type)&src
15801621
@@ -1609,6 +1650,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
16091650
return visitor.visit_load_address(self)
16101651

16111652

1653+
@final
16121654
class KeepAlive(RegisterOp):
16131655
"""A no-op operation that ensures source values aren't freed.
16141656
@@ -1656,6 +1698,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:
16561698
return visitor.visit_keep_alive(self)
16571699

16581700

1701+
@final
16591702
class Unborrow(RegisterOp):
16601703
"""A no-op op to create a regular reference from a borrowed one.
16611704

0 commit comments

Comments
 (0)