-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
genopsvisitor.py
301 lines (211 loc) · 11.5 KB
/
genopsvisitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""Dispatcher used when transforming a mypy AST to the IR form.
mypyc.genops and mypyc.genopsmain are closely related.
"""
from typing_extensions import NoReturn
from mypy.nodes import (
MypyFile, FuncDef, ReturnStmt, AssignmentStmt, OpExpr,
IntExpr, NameExpr, Var, IfStmt, UnaryExpr, ComparisonExpr, WhileStmt, CallExpr,
IndexExpr, Block, ListExpr, ExpressionStmt, MemberExpr, ForStmt,
BreakStmt, ContinueStmt, ConditionalExpr, OperatorAssignmentStmt, TupleExpr, ClassDef,
Import, ImportFrom, ImportAll, DictExpr, StrExpr, CastExpr, TempNode,
PassStmt, PromoteExpr, AssignmentExpr, AwaitExpr, BackquoteExpr, AssertStmt, BytesExpr,
ComplexExpr, Decorator, DelStmt, DictionaryComprehension, EllipsisExpr, EnumCallExpr, ExecStmt,
FloatExpr, GeneratorExpr, GlobalDecl, LambdaExpr, ListComprehension, SetComprehension,
NamedTupleExpr, NewTypeExpr, NonlocalDecl, OverloadedFuncDef, PrintStmt, RaiseStmt,
RevealExpr, SetExpr, SliceExpr, StarExpr, SuperExpr, TryStmt, TypeAliasExpr, TypeApplication,
TypeVarExpr, TypedDictExpr, UnicodeExpr, WithStmt, YieldFromExpr, YieldExpr
)
from mypyc.ops import Value
from mypyc.genops import IRVisitor, IRBuilder, UnsupportedException
from mypyc.genclass import BuildClassIR
from mypyc.genfunc import BuildFuncIR
from mypyc.genstatement import (
transform_block,
transform_expression_stmt,
transform_return_stmt,
transform_assignment_stmt,
transform_operator_assignment_stmt,
transform_if_stmt,
transform_while_stmt,
transform_for_stmt,
transform_break_stmt,
transform_continue_stmt,
transform_raise_stmt,
transform_try_stmt,
transform_with_stmt,
transform_assert_stmt,
transform_del_stmt,
)
from mypyc.genexpr import BuildExpressionIR
class IRBuilderVisitor(IRVisitor):
"""Mypy node visitor that dispatches to node transform implementations.
This class should have no non-trivial logic.
This visitor is separated from the rest of code to improve modularity and
to avoid import cycles.
This is based on the visitor pattern
(https://en.wikipedia.org/wiki/Visitor_pattern).
"""
# This gets passed to all the implementations and contains all the
# state and many helpers. The attribute is initialized outside
# this class since this class and IRBuilder form a reference loop.
builder = None # type: IRBuilder
def visit_mypy_file(self, mypyfile: MypyFile) -> None:
self.builder.visit_mypy_file(mypyfile)
def visit_class_def(self, cdef: ClassDef) -> None:
BuildClassIR(self.builder).visit_class_def(cdef)
def visit_import(self, node: Import) -> None:
self.builder.visit_import(node)
def visit_import_from(self, node: ImportFrom) -> None:
self.builder.visit_import_from(node)
def visit_import_all(self, node: ImportAll) -> None:
self.builder.visit_import_all(node)
def visit_func_def(self, fdef: FuncDef) -> None:
BuildFuncIR(self.builder).visit_func_def(fdef)
def visit_overloaded_func_def(self, o: OverloadedFuncDef) -> None:
BuildFuncIR(self.builder).visit_overloaded_func_def(o)
def visit_decorator(self, dec: Decorator) -> None:
BuildFuncIR(self.builder).visit_decorator(dec)
def visit_block(self, block: Block) -> None:
transform_block(self.builder, block)
# Statements
def visit_expression_stmt(self, stmt: ExpressionStmt) -> None:
transform_expression_stmt(self.builder, stmt)
def visit_return_stmt(self, stmt: ReturnStmt) -> None:
transform_return_stmt(self.builder, stmt)
def visit_assignment_stmt(self, stmt: AssignmentStmt) -> None:
transform_assignment_stmt(self.builder, stmt)
def visit_operator_assignment_stmt(self, stmt: OperatorAssignmentStmt) -> None:
transform_operator_assignment_stmt(self.builder, stmt)
def visit_if_stmt(self, stmt: IfStmt) -> None:
transform_if_stmt(self.builder, stmt)
def visit_while_stmt(self, stmt: WhileStmt) -> None:
transform_while_stmt(self.builder, stmt)
def visit_for_stmt(self, stmt: ForStmt) -> None:
transform_for_stmt(self.builder, stmt)
def visit_break_stmt(self, stmt: BreakStmt) -> None:
transform_break_stmt(self.builder, stmt)
def visit_continue_stmt(self, stmt: ContinueStmt) -> None:
transform_continue_stmt(self.builder, stmt)
def visit_raise_stmt(self, stmt: RaiseStmt) -> None:
transform_raise_stmt(self.builder, stmt)
def visit_try_stmt(self, stmt: TryStmt) -> None:
transform_try_stmt(self.builder, stmt)
def visit_with_stmt(self, stmt: WithStmt) -> None:
transform_with_stmt(self.builder, stmt)
def visit_pass_stmt(self, stmt: PassStmt) -> None:
pass
def visit_assert_stmt(self, stmt: AssertStmt) -> None:
transform_assert_stmt(self.builder, stmt)
def visit_del_stmt(self, stmt: DelStmt) -> None:
transform_del_stmt(self.builder, stmt)
def visit_global_decl(self, stmt: GlobalDecl) -> None:
# Pure declaration -- no runtime effect
pass
def visit_nonlocal_decl(self, stmt: NonlocalDecl) -> None:
# Pure declaration -- no runtime effect
pass
# Expressions
def visit_name_expr(self, expr: NameExpr) -> Value:
return BuildExpressionIR(self.builder).visit_name_expr(expr)
def visit_member_expr(self, expr: MemberExpr) -> Value:
return BuildExpressionIR(self.builder).visit_member_expr(expr)
def visit_super_expr(self, expr: SuperExpr) -> Value:
return BuildExpressionIR(self.builder).visit_super_expr(expr)
def visit_call_expr(self, expr: CallExpr) -> Value:
return BuildExpressionIR(self.builder).visit_call_expr(expr)
def visit_unary_expr(self, expr: UnaryExpr) -> Value:
return BuildExpressionIR(self.builder).visit_unary_expr(expr)
def visit_op_expr(self, expr: OpExpr) -> Value:
return BuildExpressionIR(self.builder).visit_op_expr(expr)
def visit_index_expr(self, expr: IndexExpr) -> Value:
return BuildExpressionIR(self.builder).visit_index_expr(expr)
def visit_conditional_expr(self, expr: ConditionalExpr) -> Value:
return BuildExpressionIR(self.builder).visit_conditional_expr(expr)
def visit_comparison_expr(self, expr: ComparisonExpr) -> Value:
return BuildExpressionIR(self.builder).visit_comparison_expr(expr)
def visit_int_expr(self, expr: IntExpr) -> Value:
return BuildExpressionIR(self.builder).visit_int_expr(expr)
def visit_float_expr(self, expr: FloatExpr) -> Value:
return BuildExpressionIR(self.builder).visit_float_expr(expr)
def visit_complex_expr(self, expr: ComplexExpr) -> Value:
return BuildExpressionIR(self.builder).visit_complex_expr(expr)
def visit_str_expr(self, expr: StrExpr) -> Value:
return BuildExpressionIR(self.builder).visit_str_expr(expr)
def visit_bytes_expr(self, expr: BytesExpr) -> Value:
return BuildExpressionIR(self.builder).visit_bytes_expr(expr)
def visit_ellipsis(self, expr: EllipsisExpr) -> Value:
return BuildExpressionIR(self.builder).visit_ellipsis(expr)
def visit_list_expr(self, expr: ListExpr) -> Value:
return BuildExpressionIR(self.builder).visit_list_expr(expr)
def visit_tuple_expr(self, expr: TupleExpr) -> Value:
return BuildExpressionIR(self.builder).visit_tuple_expr(expr)
def visit_dict_expr(self, expr: DictExpr) -> Value:
return BuildExpressionIR(self.builder).visit_dict_expr(expr)
def visit_set_expr(self, expr: SetExpr) -> Value:
return BuildExpressionIR(self.builder).visit_set_expr(expr)
def visit_list_comprehension(self, expr: ListComprehension) -> Value:
return BuildExpressionIR(self.builder).visit_list_comprehension(expr)
def visit_set_comprehension(self, expr: SetComprehension) -> Value:
return BuildExpressionIR(self.builder).visit_set_comprehension(expr)
def visit_dictionary_comprehension(self, expr: DictionaryComprehension) -> Value:
return BuildExpressionIR(self.builder).visit_dictionary_comprehension(expr)
def visit_slice_expr(self, expr: SliceExpr) -> Value:
return BuildExpressionIR(self.builder).visit_slice_expr(expr)
def visit_generator_expr(self, expr: GeneratorExpr) -> Value:
return BuildExpressionIR(self.builder).visit_generator_expr(expr)
def visit_lambda_expr(self, expr: LambdaExpr) -> Value:
return BuildFuncIR(self.builder).visit_lambda_expr(expr)
def visit_yield_expr(self, expr: YieldExpr) -> Value:
return BuildFuncIR(self.builder).visit_yield_expr(expr)
def visit_yield_from_expr(self, o: YieldFromExpr) -> Value:
return BuildFuncIR(self.builder).visit_yield_from_expr(o)
def visit_await_expr(self, o: AwaitExpr) -> Value:
return BuildFuncIR(self.builder).visit_await_expr(o)
# Unimplemented constructs
def visit_assignment_expr(self, o: AssignmentExpr) -> Value:
self.bail("I Am The Walrus (unimplemented)", o.line)
# Unimplemented constructs that shouldn't come up because they are py2 only
def visit_backquote_expr(self, o: BackquoteExpr) -> Value:
self.bail("Python 2 features are unsupported", o.line)
def visit_exec_stmt(self, o: ExecStmt) -> None:
self.bail("Python 2 features are unsupported", o.line)
def visit_print_stmt(self, o: PrintStmt) -> None:
self.bail("Python 2 features are unsupported", o.line)
def visit_unicode_expr(self, o: UnicodeExpr) -> Value:
self.bail("Python 2 features are unsupported", o.line)
# Constructs that shouldn't ever show up
def visit_enum_call_expr(self, o: EnumCallExpr) -> Value:
assert False, "can't compile analysis-only expressions"
def visit__promote_expr(self, o: PromoteExpr) -> Value:
assert False, "can't compile analysis-only expressions"
def visit_namedtuple_expr(self, o: NamedTupleExpr) -> Value:
assert False, "can't compile analysis-only expressions"
def visit_newtype_expr(self, o: NewTypeExpr) -> Value:
assert False, "can't compile analysis-only expressions"
def visit_temp_node(self, o: TempNode) -> Value:
assert False, "can't compile analysis-only expressions"
def visit_type_alias_expr(self, o: TypeAliasExpr) -> Value:
assert False, "can't compile analysis-only expressions"
def visit_type_application(self, o: TypeApplication) -> Value:
assert False, "can't compile analysis-only expressions"
def visit_type_var_expr(self, o: TypeVarExpr) -> Value:
assert False, "can't compile analysis-only expressions"
def visit_typeddict_expr(self, o: TypedDictExpr) -> Value:
assert False, "can't compile analysis-only expressions"
def visit_reveal_expr(self, o: RevealExpr) -> Value:
assert False, "can't compile analysis-only expressions"
def visit_var(self, o: Var) -> None:
assert False, "can't compile Var; should have been handled already?"
def visit_cast_expr(self, o: CastExpr) -> Value:
assert False, "CastExpr should have been handled in CallExpr"
def visit_star_expr(self, o: StarExpr) -> Value:
assert False, "should have been handled in Tuple/List/Set/DictExpr or CallExpr"
# Helpers
def bail(self, msg: str, line: int) -> NoReturn:
"""Reports an error and aborts compilation up until the last accept() call
(accept() catches the UnsupportedException and keeps on
processing. This allows errors to be non-blocking without always
needing to write handling for them.
"""
self.builder.error(msg, line)
raise UnsupportedException()