def visit_Assign(self, node): """ Create Assign node for final Cxx representation. It tries to handle multi assignment like: >> a = b = c = 2 If only one local variable is assigned, typing is added: >> int a = 2; TODO: Handle case of multi-assignement for some local variables. Finally, process OpenMP clause like #pragma omp atomic """ if not all( isinstance(n, (ast.Name, ast.Subscript)) for n in node.targets): raise PythranSyntaxError( "Must assign to an identifier or a subscript", node) value = self.visit(node.value) targets = [self.visit(t) for t in node.targets] alltargets = "= ".join(targets) islocal = (len(targets) == 1 and isinstance(node.targets[0], ast.Name) and node.targets[0].id in self.scope[node]) if islocal and not self.yields: # remove this decl from local decls tdecls = {t.id for t in node.targets} self.ldecls = {d for d in self.ldecls if d.id not in tdecls} # add a local declaration alltargets = '{} {}'.format(self.local_types[node.targets[0]], alltargets) stmt = Assign(alltargets, value) return self.process_omp_attachements(node, stmt)
def visit_Yield(self, node): num, label = self.yields[node] return "".join(n for n in Block([ Assign(CxxGenerator.StateHolder, num), ReturnStatement("{0} = {1}".format(CxxGenerator.StateValue, self.visit(node.value))), Statement("{0}:".format(label)) ]).generate())
def visit_AugAssign(self, node): value = self.visit(node.value) target = self.visit(node.target) l = operator_to_lambda[type(node.op)] if isinstance(node.op, (ast.FloorDiv, ast.Mod, ast.Pow, ast.Div)): stmt = Assign(target, l(target, value)) else: stmt = Statement(l(target, '')[1:-2] + '= {0}'.format(value)) return self.process_omp_attachements(node, stmt)
def visit_Assign(self, node): """ Create Assign node for final Cxx representation. It tries to handle multi assignment like: >> a = b = c = 2 If only one local variable is assigned, typing is added: >> int a = 2; TODO: Handle case of multi-assignement for some local variables. Finally, process OpenMP clause like #pragma omp atomic """ if not all(isinstance(n, (ast.Name, ast.Subscript)) for n in node.targets): raise PythranSyntaxError( "Must assign to an identifier or a subscript", node) value = self.visit(node.value) targets = [self.visit(t) for t in node.targets] alltargets = "= ".join(targets) islocal = (len(targets) == 1 and isinstance(node.targets[0], ast.Name) and node.targets[0].id in self.scope[node] and node.targets[0].id not in self.openmp_deps) if islocal: # remove this decls from local decls self.ldecls.difference_update(t.id for t in node.targets) # add a local declaration if self.types[node.targets[0]].iscombined(): alltargets = '{} {}'.format(self.typeof(node.targets[0]), alltargets) elif isinstance(self.types[node.targets[0]], self.types.builder.Assignable): alltargets = '{} {}'.format( self.types.builder.AssignableNoEscape( self.types.builder.NamedType( 'decltype({})'.format(value))), alltargets) else: assert isinstance(self.types[node.targets[0]], self.types.builder.Lazy) alltargets = '{} {}'.format( self.types.builder.Lazy( self.types.builder.NamedType( 'decltype({})'.format(value))), alltargets) stmt = Assign(alltargets, value) return self.process_omp_attachements(node, stmt)
def visit_Return(self, node): value = self.visit(node.value) if metadata.get(node, metadata.StaticReturn): # don't rely on auto because we want to make sure there's no # conversion each time we return # this happens for variant because the variant param # order may differ from the init order (because of the way we # do type inference rtype = "typename {}::type::result_type".format(self.fname) stmt = Block([Assign("static %s tmp_global" % rtype, value), ReturnStatement("tmp_global")]) else: stmt = ReturnStatement(value) return self.process_omp_attachements(node, stmt)
def visit_Return(self, node): if self.yields: return Block([ Statement("{0} = -1".format(Cxx.generator_state_holder)), Statement("goto {0}".format(Cxx.final_statement)) ]) else: value = self.visit(node.value) if metadata.get(node, metadata.StaticReturn): stmt = Block([ Assign("static auto tmp_global", value), ReturnStatement("tmp_global") ]) else: stmt = ReturnStatement(value) return self.process_omp_attachements(node, stmt)
def visit_Assign(self, node): value = self.visit(node.value) targets = [self.visit(t) for t in node.targets] alltargets = "= ".join(targets) stmt = Assign(alltargets, value) return self.process_omp_attachements(node, stmt)