OILS / mycpp / conversion_pass.py View on Github | oils.pub

420 lines, 238 significant
1"""
2conversion_pass.py - forward declarations, and virtuals
3"""
4import mypy
5
6from mypy.nodes import (Expression, NameExpr, MemberExpr, TupleExpr, CallExpr,
7 ClassDef, FuncDef, Argument)
8from mypy.types import Type, Instance, TupleType, NoneType, PartialType
9
10from mycpp import util
11from mycpp.util import log, SplitPyName
12from mycpp import pass_state
13from mycpp import visitor
14from mycpp import cppgen_pass
15
16from typing import Dict, List, Tuple, Optional, TYPE_CHECKING
17
18if TYPE_CHECKING:
19 #from mycpp import cppgen_pass
20 pass
21
22_ = log
23
24DotExprs = Dict[MemberExpr, pass_state.member_t]
25
26
27class MyTypeInfo:
28 """Like mypy.nodes.TypeInfo"""
29
30 def __init__(self, fullname: str) -> None:
31 self.fullname = fullname
32
33
34class Primitive(Instance):
35
36 def __init__(self, name: str, args: List[Type] = None) -> None:
37 self.type = MyTypeInfo(name) # type: ignore
38 self.args = args if args is not None else []
39
40
41MYCPP_INT = Primitive('builtins.int')
42
43
44class Pass(visitor.SimpleVisitor):
45
46 def __init__(
47 self,
48 types: Dict[Expression, Type],
49 virtual: pass_state.Virtual,
50 forward_decls: List[str],
51 all_member_vars: 'cppgen_pass.AllMemberVars',
52 all_local_vars: 'cppgen_pass.AllLocalVars',
53 module_dot_exprs: DotExprs,
54 yield_out_params: Dict[FuncDef, Tuple[str, str]], # output
55 dunder_exit_special: Dict[FuncDef, bool],
56 ) -> None:
57 visitor.SimpleVisitor.__init__(self)
58
59 # Input
60 self.types = types
61
62 # These are all outputs we compute
63 self.virtual = virtual
64 self.forward_decls = forward_decls
65 self.all_member_vars = all_member_vars
66 self.all_local_vars = all_local_vars
67 self.module_dot_exprs = module_dot_exprs
68 # Used to add another param to definition, and
69 # yield x --> YIELD->append(x)
70 self.yield_out_params = yield_out_params
71 self.dunder_exit_special = dunder_exit_special
72
73 # Internal state
74 self.inside_dunder_exit = None
75 self.current_member_vars: Dict[str, 'cppgen_pass.MemberVar'] = {}
76 self.current_local_vars: List[Tuple[str, Type]] = []
77
78 # Where do we need to update current_local_vars?
79 #
80 # x = 42 # oils_visit_assignment_stmt
81 # a, b = foo
82
83 # x = [y for y in other] # oils_visit_assign_to_listcomp_:
84 #
85 # Special case for enumerate:
86 # for i, x in enumerate(other):
87 #
88 # def f(p, q): # params are locals, _WriteFuncParams
89 # # but only if update_locals
90
91 self.imported_names = set() # MemberExpr -> module::Foo() or self->foo
92 # HACK for conditional import inside mylib.PYTHON
93 # in core/shell.py
94 self.imported_names.add('help_meta')
95
96 def visit_import(self, o: 'mypy.nodes.Import') -> None:
97 for name, as_name in o.ids:
98 if as_name is not None:
99 # import time as time_
100 self.imported_names.add(as_name)
101 else:
102 # import libc
103 self.imported_names.add(name)
104
105 def visit_import_from(self, o: 'mypy.nodes.ImportFrom') -> None:
106 """
107 Write C++ namespace aliases and 'using' for imports.
108 We need them in the 'decl' phase for default arguments like
109 runtime_asdl::scope_e -> scope_e
110 """
111 # For MemberExpr . -> module::func() or this->field. Also needed in
112 # the decl phase for default arg values.
113 for name, alias in o.names:
114 if alias:
115 self.imported_names.add(alias)
116 else:
117 self.imported_names.add(name)
118
119 def oils_visit_member_expr(self, o: 'mypy.nodes.MemberExpr') -> None:
120 # Why is self.types[o] missing some types? e.g. hnode.Record() call in
121 # asdl/runtime.py, failing with KeyError NameExpr
122 lhs_type = self.types.get(o.expr) # type: Optional[Type]
123
124 is_small_str = False
125 if util.SMALL_STR:
126 if util.IsStr(lhs_type):
127 is_small_str = True
128
129 # This is an approximate hack that assumes that locals don't shadow
130 # imported names. Might be a problem with names like 'word'?
131 if is_small_str:
132 # mystr.upper()
133 dot = pass_state.StackObjectMember(
134 o.expr, lhs_type, o.name) # type: pass_state.member_t
135
136 elif o.name in ('CreateNull', 'Take'):
137 # heuristic for MyType::CreateNull()
138 # MyType::Take(other)
139 type_name = self.types[o].ret_type.type.fullname
140 dot = pass_state.StaticClassMember(type_name, o.name)
141 elif (isinstance(o.expr, NameExpr) and
142 o.expr.name in self.imported_names):
143 # heuristic for state::Mem()
144 module_path = SplitPyName(o.expr.fullname or o.expr.name)
145 dot = pass_state.ModuleMember(module_path, o.name)
146 else:
147 # mylist->append(42)
148 dot = pass_state.HeapObjectMember(o.expr, lhs_type, o.name)
149
150 self.module_dot_exprs[o] = dot
151
152 self.accept(o.expr)
153
154 def oils_visit_mypy_file(self, o: 'mypy.nodes.MypyFile') -> None:
155 mod_parts = o.fullname.split('.')
156 comment = 'forward declare'
157
158 self.write('namespace %s { // %s\n', mod_parts[-1], comment)
159
160 # Do default traversal
161 self.indent += 1
162 super().oils_visit_mypy_file(o)
163 self.indent -= 1
164
165 self.write('}\n')
166 self.write('\n')
167
168 def oils_visit_class_def(
169 self, o: 'mypy.nodes.ClassDef',
170 base_class_sym: Optional[util.SymbolPath],
171 current_class_name: Optional[util.SymbolPath]) -> None:
172 self.write_ind('class %s;\n', o.name)
173 if base_class_sym:
174 self.virtual.OnSubclass(base_class_sym, current_class_name)
175
176 # Do default traversal of methods, associating member vars with the
177 # ClassDef node
178 self.current_member_vars = {}
179 super().oils_visit_class_def(o, base_class_sym, current_class_name)
180 self.all_member_vars[o] = self.current_member_vars
181
182 def _ValidateDefaultArg(self, arg: Argument) -> None:
183 t = self.types[arg.initializer]
184
185 valid = False
186 if isinstance(t, NoneType):
187 valid = True
188 if isinstance(t, Instance):
189 # Allowing strings since they're immutable, e.g.
190 # prefix='' seems OK
191 if t.type.fullname in ('builtins.bool', 'builtins.int',
192 'builtins.float', 'builtins.str'):
193 valid = True
194
195 # ASDL enums lex_mode_t, scope_t, ...
196 if t.type.fullname.endswith('_t'):
197 valid = True
198
199 # Hack for loc__Missing. Should detect the general case.
200 if t.type.fullname.endswith('loc__Missing'):
201 valid = True
202
203 if not valid:
204 self.report_error(
205 arg,
206 'Invalid default arg %r of type %s (not None, bool, int, float, ASDL enum)'
207 % (arg.initializer, t))
208
209 def _ValidateDefaultArgs(self, func_def: FuncDef) -> None:
210 arguments = func_def.arguments
211
212 num_defaults = 0
213 for arg in arguments:
214 if arg.initializer:
215 self._ValidateDefaultArg(arg)
216 num_defaults += 1
217
218 if num_defaults > 1:
219 # Report on first arg
220 self.report_error(
221 arg, '%s has %d default arguments. Only 1 is allowed' %
222 (func_def.name, num_defaults))
223 return
224
225 def oils_visit_func_def(self, o: 'mypy.nodes.FuncDef',
226 current_class_name: Optional[util.SymbolPath],
227 current_method_name: Optional[str]) -> None:
228 self._ValidateDefaultArgs(o)
229
230 self.virtual.OnMethod(current_class_name, o.name)
231
232 self.current_local_vars = []
233
234 # Add params as local vars, but only if we're NOT in a constructor.
235 # This is borrowed from cppgen_pass -
236 # _ConstructorImpl has update_locals=False, likewise for decl
237 # Is this just a convention?
238 # Counterexample: what if locals are used in __init__ after allocation?
239 # Are we assuming we never do mylib.MaybeCollect() inside a
240 # constructor? We can check that too.
241
242 if current_method_name != '__init__':
243 # Add function params as locals, to be rooted
244 arg_types = o.type.arg_types
245 arg_names = [arg.variable.name for arg in o.arguments]
246 for name, typ in zip(arg_names, arg_types):
247 if name == 'self':
248 continue
249 self.current_local_vars.append((name, typ))
250
251 # Traverse to collect member variables
252 super().oils_visit_func_def(o, current_class_name, current_method_name)
253 self.all_local_vars[o] = self.current_local_vars
254
255 # Is this function is a generator? Then associate the node with an
256 # accumulator param (name and type).
257 # This is info is consumed by both the Decl and Impl passes
258 _, _, c_iter_list_type = cppgen_pass.GetCReturnType(o.type.ret_type)
259 if c_iter_list_type is not None:
260 self.yield_out_params[o] = ('YIELD', c_iter_list_type)
261
262 def oils_visit_dunder_exit(self, o: ClassDef, stmt: FuncDef,
263 base_class_sym: util.SymbolPath) -> None:
264 self.inside_dunder_exit = o
265 super().oils_visit_dunder_exit(o, stmt, base_class_sym)
266 self.inside_dunder_exit = None
267
268 def visit_return_stmt(self, o: 'mypy.nodes.ReturnStmt') -> None:
269 # Mark special destructors
270 if self.inside_dunder_exit:
271 self.dunder_exit_special[self.inside_dunder_exit] = True
272 super().visit_return_stmt(o)
273
274 def visit_raise_stmt(self, o: 'mypy.nodes.RaiseStmt') -> None:
275 if self.inside_dunder_exit:
276 # Note: this doesn't check function calls that raise, but it's
277 # better than nothing
278 self.report_error(
279 o, "raise not allowed within __exit__ (C++ doesn't allow it)")
280 return
281 super().visit_raise_stmt(o)
282
283 def oils_visit_assign_to_listcomp(self, lval: NameExpr,
284 left_expr: Expression,
285 index_expr: Expression, seq: Expression,
286 cond: Expression) -> None:
287 # We need to consider 'result' a local var:
288 # result = [x for x in other]
289
290 # what about yield accumulator, like
291 # it_g = g(n)
292 self.current_local_vars.append((lval.name, self.types[lval]))
293
294 super().oils_visit_assign_to_listcomp(lval, left_expr, index_expr, seq,
295 cond)
296
297 def _MaybeAddMember(self, lval: MemberExpr,
298 current_method_name: Optional[str],
299 at_global_scope: bool) -> None:
300 assert not at_global_scope, "Members shouldn't be assigned at the top level"
301
302 # Collect statements that look like self.foo = 1
303 # Only do this in __init__ so that a derived class mutating a field
304 # from the base class doesn't cause duplicate C++ fields. (C++
305 # allows two fields of the same name!)
306 #
307 # HACK for WordParser: also include Reset(). We could change them
308 # all up front but I kinda like this.
309 if current_method_name not in ('__init__', 'Reset'):
310 return
311
312 if isinstance(lval.expr, NameExpr) and lval.expr.name == 'self':
313 #log(' lval.name %s', lval.name)
314 lval_type = self.types[lval]
315 c_type = cppgen_pass.GetCType(lval_type)
316 is_managed = cppgen_pass.CTypeIsManaged(c_type)
317 self.current_member_vars[lval.name] = (lval_type, c_type,
318 is_managed)
319
320 def oils_visit_assignment_stmt(self, o: 'mypy.nodes.AssignmentStmt',
321 lval: Expression, rval: Expression,
322 current_method_name: Optional[str],
323 at_global_scope: bool) -> None:
324
325 if isinstance(lval, MemberExpr):
326 self._MaybeAddMember(lval, current_method_name, at_global_scope)
327
328 if lval in self.types and isinstance(self.types[lval], PartialType):
329 t = self.types[lval]
330 self.report_error(
331 o,
332 "Mismatched types: trying to assign expression of type '%s' to "
333 "a PartialType variable '%s' (was likely assigned None before).\n"
334 "Tip: If your type translates to a heap-allocated type (e.g. str "
335 "or class), you can annotate it with '# type: Optional[T]'. "
336 "If your type is allocated on the stack (int), then you can use "
337 "-1 or similar as an in-band null value" %
338 (t.var.type.type.name, t.var.name))
339 return
340
341 # Handle:
342 # x = y
343 # These two are special cases in cppgen_pass, but not here
344 # x = NewDict()
345 # x = cast(T, y)
346 #
347 # Note: this has duplicates: the 'done' set in visit_block() handles
348 # it. Could make it a Dict.
349 if isinstance(lval, NameExpr):
350 rval_type = self.types[rval]
351
352 # Two pieces of logic adapted from cppgen_pass: is_iterator and is_cast.
353 # Can we simplify them?
354
355 is_iterator = (isinstance(rval_type, Instance) and
356 rval_type.type.fullname == 'typing.Iterator')
357
358 # Downcasted vars are BLOCK-scoped, not FUNCTION-scoped, so they
359 # don't become local vars. They are also ALIASED, so they don't
360 # need to be rooted.
361 is_downcast_and_shadow = False
362 if (isinstance(rval, CallExpr) and
363 isinstance(rval.callee, NameExpr) and
364 rval.callee.name == 'cast'):
365 to_cast = rval.args[1]
366 if (isinstance(to_cast, NameExpr) and
367 to_cast.name.startswith('UP_')):
368 is_downcast_and_shadow = True
369
370 if (not at_global_scope and not is_iterator and
371 not is_downcast_and_shadow):
372 self.current_local_vars.append((lval.name, self.types[lval]))
373
374 # Handle local vars, like _write_tuple_unpacking
375
376 # This handles:
377 # a, b = func_that_returns_tuple()
378 if isinstance(lval, TupleExpr):
379 if isinstance(rval, TupleExpr):
380 self.report_error(
381 o, "mycpp currently does not handle code like "
382 "'a, b = x, y'. You can move each assignment to its own line "
383 "or return (x, y) from a function instead.")
384 return
385
386 rval_type = self.types[rval]
387 assert isinstance(rval_type, TupleType), rval_type
388
389 for i, (lval_item,
390 item_type) in enumerate(zip(lval.items, rval_type.items)):
391 #self.log('*** %s :: %s', lval_item, item_type)
392 if isinstance(lval_item, NameExpr):
393 if util.SkipAssignment(lval_item.name):
394 continue
395 self.current_local_vars.append((lval_item.name, item_type))
396
397 # self.a, self.b = foo()
398 if isinstance(lval_item, MemberExpr):
399 self._MaybeAddMember(lval_item, current_method_name,
400 at_global_scope)
401
402 super().oils_visit_assignment_stmt(o, lval, rval, current_method_name,
403 at_global_scope)
404
405 def oils_visit_for_stmt(self, o: 'mypy.nodes.ForStmt',
406 func_name: Optional[str]) -> None:
407 # TODO: this variable should be BLOCK scoped, not function scoped, like
408 # the tuple variables for i, x
409 index0_name: Optional[str] = None
410 if func_name == 'enumerate':
411 assert isinstance(o.index, TupleExpr), o.index
412 index0 = o.index.items[0]
413 assert isinstance(index0, NameExpr), index0
414 index0_name = index0.name # generate int i = 0; ; ++i
415
416 if index0_name:
417 # can't initialize two things in a for loop, so do it on a separate line
418 self.current_local_vars.append((index0_name, MYCPP_INT))
419
420 super().oils_visit_for_stmt(o, func_name)