def translate_stmt_Try(self, node: ast.Try, ctx: Context) -> List[Stmt]: no_pos = self.no_position(ctx) no_info = self.no_info(ctx) try_block = self._get_try_block(node, ctx) assert try_block body = flatten([self.translate_stmt(stmt, ctx) for stmt in node.body]) if try_block.else_block: else_block = flatten([ self.translate_stmt(stmt, ctx) for stmt in try_block.else_block.body ]) else_block = self.viper.Seqn(else_block, no_pos, no_info) else: else_block = None if try_block.finally_block: finally_block = flatten([ self.translate_stmt(stmt, ctx) for stmt in try_block.finally_block ]) finally_block = self.viper.Seqn(finally_block, no_pos, no_info) else: finally_block = None catch_blocks = self._create_try_handlers(node, try_block, ctx) return [ self.viper.Try(self.viper.Seqn(body, no_pos, no_info), catch_blocks, else_block, finally_block, self.to_position(node, ctx), no_info) ]
def translate_pure_If(self, conds: List, node: ast.If, ctx: Context) -> List[Wrapper]: """ Translates an if-block to a list of Return- and AssignWrappers which contain the condition(s) introduced by the if-block. """ cond = node.test cond_var = ctx.current_function.create_variable( 'cond', ctx.module.global_module.classes[BOOL_TYPE], self.translator) cond_let = AssignWrapper(cond_var.sil_name, conds, cond, node) then_cond = conds + [cond_var.sil_name] else_cond = conds + [NotWrapper(cond_var.sil_name)] then = [ self.translate_pure(then_cond, stmt, ctx) for stmt in node.body ] then = flatten(then) else_ = [] if node.orelse: else_ = [ self.translate_pure(else_cond, stmt, ctx) for stmt in node.orelse ] else_ = flatten(else_) return [cond_let] + then + else_
def _create_try_handlers( self, node: ast.Try, try_block: PythonTryBlock, ctx: Context) -> List['silver.sif.SIFExceptionHandler']: no_pos = self.no_position(ctx) no_info = self.no_info(ctx) catch_blocks = [] error_var = self.get_error_var(node, ctx) for handler in try_block.handlers: error_type_check = self.type_check(error_var.ref(), handler.exception, self.to_position( handler.node, ctx), ctx, inhale_exhale=False) handler_body = [] if handler.exception_name: ctx.var_aliases[handler.exception_name] = error_var error_var.type = handler.exception handler_body.append( self.set_var_defined(error_var, no_pos, no_info)) handler_body += flatten( [self.translate_stmt(stmt, ctx) for stmt in handler.body]) handler_body_seqn = self.viper.Seqn(handler_body, no_pos, no_info) catch_blocks.append( self.viper.SIFExceptionHandler(error_var.ref(), error_type_check, handler_body_seqn)) if handler.exception_name: del ctx.var_aliases[handler.exception_name] return catch_blocks
def _translate_method_body(self, method: PythonMethod, ctx: Context) -> List[Stmt]: body = [] statements = method.node.body body_start, body_end = get_body_indices(statements) # Create local variables for parameters body.extend(self._create_local_vars_for_params(method, ctx)) body += flatten( [self.translate_stmt(stmt, ctx) for stmt in method.node.body[body_start:body_end]]) return body
def _translate_With_body(self, try_block: PythonTryBlock, enter_res: PythonVar, ctx: Context) -> List[Stmt]: no_pos = self.no_position(ctx) no_info = self.no_info(ctx) if try_block.with_item.optional_vars: as_expr = try_block.with_item.optional_vars as_var = ctx.current_function.get_variable(as_expr.id) enter_assign = self.viper.LocalVarAssign( as_var.ref(as_expr, ctx), enter_res.ref(), self.to_position(as_expr, ctx), no_info) define_var = self.set_var_defined(as_var, no_pos, no_info) body = [enter_assign, define_var] else: body = [] body += flatten( [self.translate_stmt(stmt, ctx) for stmt in try_block.node.body]) return body
def _translate_to_wrappers(self, nodes: List[ast.AST], ctx: Context) -> List[Wrapper]: return flatten([self.translate_pure([], node, ctx) for node in nodes])
def _translate_while_body(self, node: ast.While, ctx: Context, end_label: str) -> List[Stmt]: start, end = get_body_indices(node.body) body = flatten( [self.translate_stmt(stmt, ctx) for stmt in node.body[start:end]]) return body