Skip to content

Commit bc590e4

Browse files
authored
[mypyc] Improve docstrings and comments in mypyc.ir (#8514)
Added lots of docstrings and comments, and also added detail to existing documentation. Also did some minor refactoring (just moving things around).
1 parent a0c5dfc commit bc590e4

File tree

4 files changed

+254
-76
lines changed

4 files changed

+254
-76
lines changed

mypyc/ir/class_ir.py

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -86,47 +86,12 @@
8686
VTableEntries = List[VTableEntry]
8787

8888

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-
12589
class ClassIR:
12690
"""Intermediate representation of a class.
12791
12892
This also describes the runtime structure of native instances.
12993
"""
94+
13095
def __init__(self, name: str, module_name: str, is_trait: bool = False,
13196
is_generated: bool = False, is_abstract: bool = False,
13297
is_ext_class: bool = True) -> None:
@@ -139,15 +104,17 @@ def __init__(self, name: str, module_name: str, is_trait: bool = False,
139104
# An augmented class has additional methods separate from what mypyc generates.
140105
# Right now the only one is dataclasses.
141106
self.is_augmented = False
107+
# Does this inherit from a Python class?
142108
self.inherits_python = False
109+
# Do instances of this class have __dict__?
143110
self.has_dict = False
144111
# Do we allow interpreted subclasses? Derived from a mypyc_attr.
145112
self.allow_interpreted_subclasses = False
146113
# If this a subclass of some built-in python class, the name
147114
# of the object for that class. We currently only support this
148115
# in a few ad-hoc cases.
149116
self.builtin_base = None # type: Optional[str]
150-
# Default empty ctor
117+
# Default empty constructor
151118
self.ctor = FuncDecl(name, None, module_name, FuncSignature([], RInstance(self)))
152119

153120
self.attributes = OrderedDict() # type: OrderedDict[str, RType]
@@ -398,8 +365,7 @@ def deserialize(cls, data: JsonDict, ctx: 'DeserMaps') -> 'ClassIR':
398365

399366

400367
class NonExtClassInfo:
401-
"""Information needed to construct a non-extension class.
402-
368+
"""Information needed to construct a non-extension class (Python class).
403369
404370
Includes the class dictionary, a tuple of base classes,
405371
the class annotations dictionary, and the metaclass.
@@ -412,6 +378,42 @@ def __init__(self, dict: Value, bases: Value, anns: Value, metaclass: Value) ->
412378
self.metaclass = metaclass
413379

414380

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+
415417
def all_concrete_classes(class_ir: ClassIR) -> Optional[List[ClassIR]]:
416418
"""Return all concrete classes among the class itself and its subclasses."""
417419
concrete = class_ir.concrete_subclasses()

mypyc/ir/func_ir.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Intermediate representation of functions."""
2+
13
from typing import List, Optional, Sequence, Dict
24
from typing_extensions import Final
35

@@ -12,6 +14,11 @@
1214

1315

1416
class RuntimeArg:
17+
"""Representation of a function argument in IR.
18+
19+
Argument kind is one of ARG_* constants defined in mypy.nodes.
20+
"""
21+
1522
def __init__(self, name: str, typ: RType, kind: int = ARG_POS) -> None:
1623
self.name = name
1724
self.type = typ
@@ -37,7 +44,10 @@ def deserialize(cls, data: JsonDict, ctx: DeserMaps) -> 'RuntimeArg':
3744

3845

3946
class FuncSignature:
40-
# TODO: track if method?
47+
"""Signature of a function in IR."""
48+
49+
# TODO: Track if method?
50+
4151
def __init__(self, args: Sequence[RuntimeArg], ret_type: RType) -> None:
4252
self.args = tuple(args)
4353
self.ret_type = ret_type
@@ -62,6 +72,12 @@ def deserialize(cls, data: JsonDict, ctx: DeserMaps) -> 'FuncSignature':
6272

6373

6474
class FuncDecl:
75+
"""Declaration of a function in IR (without body or implementation).
76+
77+
A function can be a regular module-level function, a method, a
78+
static method, a class method, or a property getter/setter.
79+
"""
80+
6581
def __init__(self,
6682
name: str,
6783
class_name: Optional[str],
@@ -129,7 +145,11 @@ def deserialize(cls, data: JsonDict, ctx: DeserMaps) -> 'FuncDecl':
129145

130146

131147
class FuncIR:
132-
"""Intermediate representation of a function with contextual information."""
148+
"""Intermediate representation of a function with contextual information.
149+
150+
Unlike FuncDecl, this includes the IR of the body (basic blocks) and an
151+
environment.
152+
"""
133153

134154
def __init__(self,
135155
decl: FuncDecl,
@@ -199,6 +219,7 @@ def deserialize(cls, data: JsonDict, ctx: DeserMaps) -> 'FuncIR':
199219

200220

201221
def format_blocks(blocks: List[BasicBlock], env: Environment) -> List[str]:
222+
"""Format a list of IR basic blocks into a human-readable form."""
202223
# First label all of the blocks
203224
for i, block in enumerate(blocks):
204225
block.label = i

0 commit comments

Comments
 (0)