86
86
VTableEntries = List [VTableEntry ]
87
87
88
88
89
- def serialize_vtable_entry (entry : VTableEntry ) -> JsonDict :
90
- if isinstance (entry , VTableMethod ):
91
- return {
92
- '.class' : 'VTableMethod' ,
93
- 'cls' : entry .cls .fullname ,
94
- 'name' : entry .name ,
95
- 'method' : entry .method .decl .fullname ,
96
- 'shadow_method' : entry .shadow_method .decl .fullname if entry .shadow_method else None ,
97
- }
98
- else :
99
- return {
100
- '.class' : 'VTableAttr' ,
101
- 'cls' : entry .cls .fullname ,
102
- 'name' : entry .name ,
103
- 'is_setter' : entry .is_setter ,
104
- }
105
-
106
-
107
- def serialize_vtable (vtable : VTableEntries ) -> List [JsonDict ]:
108
- return [serialize_vtable_entry (v ) for v in vtable ]
109
-
110
-
111
- def deserialize_vtable_entry (data : JsonDict , ctx : 'DeserMaps' ) -> VTableEntry :
112
- if data ['.class' ] == 'VTableMethod' :
113
- return VTableMethod (
114
- ctx .classes [data ['cls' ]], data ['name' ], ctx .functions [data ['method' ]],
115
- ctx .functions [data ['shadow_method' ]] if data ['shadow_method' ] else None )
116
- elif data ['.class' ] == 'VTableAttr' :
117
- return VTableAttr (ctx .classes [data ['cls' ]], data ['name' ], data ['is_setter' ])
118
- assert False , "Bogus vtable .class: %s" % data ['.class' ]
119
-
120
-
121
- def deserialize_vtable (data : List [JsonDict ], ctx : 'DeserMaps' ) -> VTableEntries :
122
- return [deserialize_vtable_entry (x , ctx ) for x in data ]
123
-
124
-
125
89
class ClassIR :
126
90
"""Intermediate representation of a class.
127
91
128
92
This also describes the runtime structure of native instances.
129
93
"""
94
+
130
95
def __init__ (self , name : str , module_name : str , is_trait : bool = False ,
131
96
is_generated : bool = False , is_abstract : bool = False ,
132
97
is_ext_class : bool = True ) -> None :
@@ -139,15 +104,17 @@ def __init__(self, name: str, module_name: str, is_trait: bool = False,
139
104
# An augmented class has additional methods separate from what mypyc generates.
140
105
# Right now the only one is dataclasses.
141
106
self .is_augmented = False
107
+ # Does this inherit from a Python class?
142
108
self .inherits_python = False
109
+ # Do instances of this class have __dict__?
143
110
self .has_dict = False
144
111
# Do we allow interpreted subclasses? Derived from a mypyc_attr.
145
112
self .allow_interpreted_subclasses = False
146
113
# If this a subclass of some built-in python class, the name
147
114
# of the object for that class. We currently only support this
148
115
# in a few ad-hoc cases.
149
116
self .builtin_base = None # type: Optional[str]
150
- # Default empty ctor
117
+ # Default empty constructor
151
118
self .ctor = FuncDecl (name , None , module_name , FuncSignature ([], RInstance (self )))
152
119
153
120
self .attributes = OrderedDict () # type: OrderedDict[str, RType]
@@ -398,8 +365,7 @@ def deserialize(cls, data: JsonDict, ctx: 'DeserMaps') -> 'ClassIR':
398
365
399
366
400
367
class NonExtClassInfo :
401
- """Information needed to construct a non-extension class.
402
-
368
+ """Information needed to construct a non-extension class (Python class).
403
369
404
370
Includes the class dictionary, a tuple of base classes,
405
371
the class annotations dictionary, and the metaclass.
@@ -412,6 +378,42 @@ def __init__(self, dict: Value, bases: Value, anns: Value, metaclass: Value) ->
412
378
self .metaclass = metaclass
413
379
414
380
381
+ def serialize_vtable_entry (entry : VTableEntry ) -> JsonDict :
382
+ if isinstance (entry , VTableMethod ):
383
+ return {
384
+ '.class' : 'VTableMethod' ,
385
+ 'cls' : entry .cls .fullname ,
386
+ 'name' : entry .name ,
387
+ 'method' : entry .method .decl .fullname ,
388
+ 'shadow_method' : entry .shadow_method .decl .fullname if entry .shadow_method else None ,
389
+ }
390
+ else :
391
+ return {
392
+ '.class' : 'VTableAttr' ,
393
+ 'cls' : entry .cls .fullname ,
394
+ 'name' : entry .name ,
395
+ 'is_setter' : entry .is_setter ,
396
+ }
397
+
398
+
399
+ def serialize_vtable (vtable : VTableEntries ) -> List [JsonDict ]:
400
+ return [serialize_vtable_entry (v ) for v in vtable ]
401
+
402
+
403
+ def deserialize_vtable_entry (data : JsonDict , ctx : 'DeserMaps' ) -> VTableEntry :
404
+ if data ['.class' ] == 'VTableMethod' :
405
+ return VTableMethod (
406
+ ctx .classes [data ['cls' ]], data ['name' ], ctx .functions [data ['method' ]],
407
+ ctx .functions [data ['shadow_method' ]] if data ['shadow_method' ] else None )
408
+ elif data ['.class' ] == 'VTableAttr' :
409
+ return VTableAttr (ctx .classes [data ['cls' ]], data ['name' ], data ['is_setter' ])
410
+ assert False , "Bogus vtable .class: %s" % data ['.class' ]
411
+
412
+
413
+ def deserialize_vtable (data : List [JsonDict ], ctx : 'DeserMaps' ) -> VTableEntries :
414
+ return [deserialize_vtable_entry (x , ctx ) for x in data ]
415
+
416
+
415
417
def all_concrete_classes (class_ir : ClassIR ) -> Optional [List [ClassIR ]]:
416
418
"""Return all concrete classes among the class itself and its subclasses."""
417
419
concrete = class_ir .concrete_subclasses ()
0 commit comments