1
1
"""Transform class definitions from the mypy AST form to IR."""
2
2
3
- from typing import List , Optional
3
+ from typing import List , Optional , Tuple
4
4
5
5
from mypy .nodes import (
6
6
ClassDef , FuncDef , OverloadedFuncDef , PassStmt , AssignmentStmt , NameExpr , StrExpr ,
11
11
BasicBlock , Branch , MethodCall , NAMESPACE_TYPE , LoadAddress
12
12
)
13
13
from mypyc .ir .rtypes import (
14
- object_rprimitive , bool_rprimitive , dict_rprimitive , is_optional_type ,
14
+ RType , object_rprimitive , bool_rprimitive , dict_rprimitive , is_optional_type ,
15
15
is_object_rprimitive , is_none_rprimitive
16
16
)
17
17
from mypyc .ir .func_ir import FuncDecl , FuncSignature
@@ -76,7 +76,7 @@ def transform_class_def(builder: IRBuilder, cdef: ClassDef) -> None:
76
76
dataclass_non_ext = None
77
77
type_obj = None
78
78
79
- attrs_to_cache = [] # type: List[Lvalue]
79
+ attrs_to_cache = [] # type: List[Tuple[ Lvalue, RType] ]
80
80
81
81
for stmt in cdef .defs .body :
82
82
if isinstance (stmt , OverloadedFuncDef ) and stmt .is_property :
@@ -269,7 +269,7 @@ def add_non_ext_class_attr(builder: IRBuilder,
269
269
lvalue : NameExpr ,
270
270
stmt : AssignmentStmt ,
271
271
cdef : ClassDef ,
272
- attr_to_cache : List [Lvalue ]) -> None :
272
+ attr_to_cache : List [Tuple [ Lvalue , RType ] ]) -> None :
273
273
"""Add a class attribute to __annotations__ of a non-extension class.
274
274
275
275
If the attribute is initialized with a value, also add it to __dict__.
@@ -294,7 +294,8 @@ def add_non_ext_class_attr(builder: IRBuilder,
294
294
# Skip "_order_" and "__order__", since Enum will remove it
295
295
and lvalue .name not in ('_order_' , '__order__' )
296
296
):
297
- attr_to_cache .append (lvalue )
297
+ # Enum values are always boxed, so use object_rprimitive.
298
+ attr_to_cache .append ((lvalue , object_rprimitive ))
298
299
299
300
300
301
def generate_attr_defaults (builder : IRBuilder , cdef : ClassDef ) -> None :
@@ -419,13 +420,15 @@ def load_decorated_class(builder: IRBuilder, cdef: ClassDef, type_obj: Value) ->
419
420
return dec_class
420
421
421
422
422
- def cache_class_attrs (builder : IRBuilder , attrs_to_cache : List [Lvalue ], cdef : ClassDef ) -> None :
423
+ def cache_class_attrs (builder : IRBuilder ,
424
+ attrs_to_cache : List [Tuple [Lvalue , RType ]],
425
+ cdef : ClassDef ) -> None :
423
426
"""Add class attributes to be cached to the global cache."""
424
427
typ = builder .load_native_type_object (cdef .fullname )
425
- for lval in attrs_to_cache :
428
+ for lval , rtype in attrs_to_cache :
426
429
assert isinstance (lval , NameExpr )
427
430
rval = builder .py_get_attr (typ , lval .name , cdef .line )
428
- builder .init_final_static (lval , rval , cdef .name )
431
+ builder .init_final_static (lval , rval , cdef .name , type_override = rtype )
429
432
430
433
431
434
def create_mypyc_attrs_tuple (builder : IRBuilder , ir : ClassIR , line : int ) -> Value :
0 commit comments