Skip to content

Commit d9e209f

Browse files
authored
[mypyc] Refactor: remove cyclic deps by extracting genops visitor (python#8430)
Add a simple visitor module that doesn't contain any implementation and just dispatches to various transform implementations. The main benefit is that the implementations don't need to depend on the visitor (and thus also don't need to depend on the other implementations) and that `IRBuilder` doesn't need to depend on the implementations. Also extract the driver function to another module, as this would otherwise cause an import cycle. Some cyclic dependencies remain and wouldn't be hard to get rid of. I wanted to keep this PR relatively simple so I didn't fix them yet. Work on mypyc/mypyc#714.
1 parent 7e152fa commit d9e209f

File tree

10 files changed

+428
-352
lines changed

10 files changed

+428
-352
lines changed

mypyc/emitmodule.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
from mypy.fscache import FileSystemCache
2020
from mypy.util import hash_digest
2121

22-
from mypyc import genops
22+
from mypyc import genopsmain
2323
from mypyc.genopsprepare import load_type_map
24+
from mypyc.genopsmapper import Mapper
2425
from mypyc.common import (
2526
PREFIX, TOP_LEVEL_NAME, INT_PREFIX, MODULE_PREFIX, shared_lib_name,
2627
)
@@ -178,7 +179,7 @@ def parse_and_typecheck(
178179
def compile_scc_to_ir(
179180
scc: List[MypyFile],
180181
result: BuildResult,
181-
mapper: genops.Mapper,
182+
mapper: Mapper,
182183
compiler_options: CompilerOptions,
183184
errors: Errors,
184185
) -> ModuleIRs:
@@ -201,7 +202,7 @@ def compile_scc_to_ir(
201202
print("Compiling {}".format(", ".join(x.name for x in scc)))
202203

203204
# Generate basic IR, with missing exception and refcount handling.
204-
modules = genops.build_ir(
205+
modules = genopsmain.build_ir(
205206
scc, result.graph, result.types, mapper, compiler_options, errors
206207
)
207208
if errors.num_errors > 0:
@@ -225,7 +226,7 @@ def compile_scc_to_ir(
225226

226227
def compile_modules_to_ir(
227228
result: BuildResult,
228-
mapper: genops.Mapper,
229+
mapper: Mapper,
229230
compiler_options: CompilerOptions,
230231
errors: Errors,
231232
) -> ModuleIRs:
@@ -260,7 +261,7 @@ def compile_ir_to_c(
260261
groups: Groups,
261262
modules: ModuleIRs,
262263
result: BuildResult,
263-
mapper: genops.Mapper,
264+
mapper: Mapper,
264265
compiler_options: CompilerOptions,
265266
) -> Dict[Optional[str], List[Tuple[str, str]]]:
266267
"""Compile a collection of ModuleIRs to C source text.
@@ -358,7 +359,7 @@ def write_cache(
358359
def load_scc_from_cache(
359360
scc: List[MypyFile],
360361
result: BuildResult,
361-
mapper: genops.Mapper,
362+
mapper: Mapper,
362363
ctx: DeserMaps,
363364
) -> ModuleIRs:
364365
"""Load IR for an SCC of modules from the cache.
@@ -401,7 +402,7 @@ def compile_modules_to_c(
401402
"""
402403
# Construct a map from modules to what group they belong to
403404
group_map = {source.module: lib_name for group, lib_name in groups for source in group}
404-
mapper = genops.Mapper(group_map)
405+
mapper = Mapper(group_map)
405406

406407
modules = compile_modules_to_ir(result, mapper, compiler_options, errors)
407408
ctext = compile_ir_to_c(groups, modules, result, mapper, compiler_options)

mypyc/genclass.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import List, Optional, Union
2-
from typing_extensions import overload, TYPE_CHECKING
2+
from typing_extensions import overload
33

44
from mypy.nodes import (
55
ClassDef, FuncDef, OverloadedFuncDef, PassStmt, AssignmentStmt, NameExpr, StrExpr,
@@ -23,14 +23,13 @@
2323
from mypyc.genopsutil import (
2424
is_dataclass_decorator, get_func_def, is_dataclass, is_constant, add_self_to_env
2525
)
26+
from mypyc.genfunc import BuildFuncIR
2627
from mypyc.common import SELF_NAME
27-
28-
if TYPE_CHECKING:
29-
from mypyc.genops import IRBuilder
28+
from mypyc.genops import IRBuilder
3029

3130

3231
class BuildClassIR:
33-
def __init__(self, builder: 'IRBuilder') -> None:
32+
def __init__(self, builder: IRBuilder) -> None:
3433
self.builder = builder
3534
self.mapper = builder.mapper
3635
self.module_name = builder.module_name
@@ -82,15 +81,15 @@ def visit_class_def(self, cdef: ClassDef) -> None:
8281
stmt.line)
8382
for item in stmt.items:
8483
with self.builder.catch_errors(stmt.line):
85-
self.builder.visit_method(cdef, non_ext, get_func_def(item))
84+
BuildFuncIR(self.builder).visit_method(cdef, non_ext, get_func_def(item))
8685
elif isinstance(stmt, (FuncDef, Decorator, OverloadedFuncDef)):
8786
# Ignore plugin generated methods (since they have no
8887
# bodies to compile and will need to have the bodies
8988
# provided by some other mechanism.)
9089
if cdef.info.names[stmt.name].plugin_generated:
9190
continue
9291
with self.builder.catch_errors(stmt.line):
93-
self.builder.visit_method(cdef, non_ext, get_func_def(stmt))
92+
BuildFuncIR(self.builder).visit_method(cdef, non_ext, get_func_def(stmt))
9493
elif isinstance(stmt, PassStmt):
9594
continue
9695
elif isinstance(stmt, AssignmentStmt):
@@ -417,7 +416,7 @@ def load_decorated_class(self, cdef: ClassDef, type_obj: Value) -> Value:
417416
decorators = cdef.decorators
418417
dec_class = type_obj
419418
for d in reversed(decorators):
420-
decorator = d.accept(self.builder)
419+
decorator = d.accept(self.builder.visitor)
421420
assert isinstance(decorator, Value)
422421
dec_class = self.builder.py_call(decorator, [dec_class], dec_class.line)
423422
return dec_class

mypyc/genexpr.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55

66
from typing import List, Optional, Union
7-
from typing_extensions import TYPE_CHECKING
87

98
from mypy.nodes import (
109
Expression, NameExpr, MemberExpr, SuperExpr, CallExpr, UnaryExpr, OpExpr, IndexExpr,
@@ -25,13 +24,11 @@
2524
from mypyc.ops_dict import new_dict_op, dict_set_item_op
2625
from mypyc.ops_set import new_set_op, set_add_op, set_update_op
2726
from mypyc.specialize import specializers
28-
29-
if TYPE_CHECKING:
30-
from mypyc.genops import IRBuilder
27+
from mypyc.genops import IRBuilder
3128

3229

3330
class BuildExpressionIR:
34-
def __init__(self, builder: 'IRBuilder') -> None:
31+
def __init__(self, builder: IRBuilder) -> None:
3532
self.builder = builder
3633

3734
# Name and attribute references

mypyc/genfunc.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55

66
from typing import Optional, List, Tuple, Union
7-
from typing_extensions import TYPE_CHECKING
87

98
from mypy.nodes import (
109
ClassDef, FuncDef, OverloadedFuncDef, Decorator, Var, YieldFromExpr, AwaitExpr, YieldExpr,
@@ -31,13 +30,11 @@
3130
from mypyc.genopsutil import concrete_arg_kind, is_constant, add_self_to_env
3231
from mypyc.genopscontext import FuncInfo, GeneratorClass, ImplicitClass
3332
from mypyc.genstatement import BuildStatementIR
34-
35-
if TYPE_CHECKING:
36-
from mypyc.genops import IRBuilder
33+
from mypyc.genops import IRBuilder
3734

3835

3936
class BuildFuncIR:
40-
def __init__(self, builder: 'IRBuilder') -> None:
37+
def __init__(self, builder: IRBuilder) -> None:
4138
self.builder = builder
4239
self.module_name = builder.module_name
4340
self.functions = builder.functions
@@ -878,7 +875,7 @@ def load_decorated_func(self, fdef: FuncDef, orig_func_reg: Value) -> Value:
878875
decorators = self.builder.fdefs_to_decorators[fdef]
879876
func_reg = orig_func_reg
880877
for d in reversed(decorators):
881-
decorator = d.accept(self.builder)
878+
decorator = d.accept(self.builder.visitor)
882879
assert isinstance(decorator, Value)
883880
func_reg = self.builder.py_call(decorator, [func_reg], func_reg.line)
884881
return func_reg

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy