def translate_get_old(self, node: ast.Call, ctx: Context) -> StmtsAndExpr: stmt, thread = self.translate_expr(node.args[0], ctx) pos = self.to_position(node, ctx) info = self.no_info(ctx) old_string = pprint(node.args[1]) index = self._get_string_value(old_string) index = self.viper.IntLit(index, pos, info) func = self.viper.DomainFuncApp(GET_OLD_FUNC, [thread, index], self.viper.Ref, pos, info, THREAD_DOMAIN) return stmt, func
def _to_position( self, node: ast.AST = None, conversion_rules: Rules = None, error_node: Union[str, ast.AST] = None) -> Position: error_string = None if error_node is not None: if isinstance(error_node, ast.AST): error_string = pprint(error_node) else: error_string = error_node return self._translator.to_position( node or self._default_position_node, self._ctx, error_string=error_string, rules=conversion_rules)
def translate_old(self, node: ast.Call, ctx: Context) -> StmtsAndExpr: """ Translates a call to the Old() contract function. """ if len(node.args) != 1: raise InvalidProgramException(node, 'invalid.contract.call') if ctx.old_expr_aliases: normalizer = OldExpressionTransformer() normalizer.arg_names = [a for a in ctx.actual_function._args] normalized = normalizer.visit(copy.deepcopy(node.args[0])) key = pprint(normalized) if key in ctx.old_expr_aliases: return [], ctx.old_expr_aliases[key] stmt, exp = self.translate_expr(node.args[0], ctx) res = self.viper.Old(exp, self.to_position(node, ctx), self.no_info(ctx)) return stmt, res
def _remember_old_expressions(self, method: PythonMethod, collector: OldExpressionCollector) -> Expr: """ Creates an assertion that connects all old expressions in the method's postcondition to the thread object. """ old_info = self.viper.TrueLit(self._position, self._info) normalizer = OldExpressionTransformer() normalizer.arg_names = [arg for arg in method._args] for expr in collector.expressions: print_expr = normalizer.visit(copy.deepcopy(expr)) name = pprint(print_expr) id = self.viper.IntLit(self._translator._get_string_value(name), self._position, self._info) old_func = self.viper.DomainFuncApp(GET_OLD_FUNC, [self._thread, id], self.viper.Ref, self._position, self._info, THREAD_DOMAIN) _, old_val = self._translator.translate_expr(expr, self._ctx) func_val = self.viper.EqCmp(old_func, old_val, self._position, self._info) old_info = self.viper.And(old_info, func_val, self._position, self._info) return old_info
get_target_name, pprint, ) ERRORS = { 'assignment.failed': lambda n: 'Assignment might fail.', 'call.failed': lambda n: 'Method call might fail.', 'not.wellformed': lambda n: 'Contract might not be well-formed.', 'call.precondition': lambda n: ('The precondition of method {} might not ' 'hold.'.format(get_target_name(n)) if isinstance(n, (ast.Call, ast.FunctionDef)) else 'The precondition of {} might not hold.'.format(pprint(n))), 'application.precondition': lambda n: ('The precondition of function {} might not ' 'hold.'.format(get_target_name(n)) if isinstance(n, (ast.Call, ast.FunctionDef)) else 'The precondition of {} might not hold.'.format(pprint(n))), 'exhale.failed': lambda n: 'Exhale might fail.', 'inhale.failed': lambda n: 'Inhale might fail.', 'if.failed': lambda n: 'Conditional statement might fail.', 'while.failed': lambda n: 'While statement might fail.', 'assert.failed': lambda n: 'Assert might fail.',