nitc :: BottomMContract :: inject_result
result
variable into the n_block
of the given n_mpropdef`.The purpose of the variable is to capture return values to use it in contracts.
# Inject the `result` variable into the `n_block` of the given n_mpropdef`.
#
# The purpose of the variable is to capture return values to use it in contracts.
private fun inject_result(v: ContractsVisitor, n_mpropdef: AMethPropdef, ret_type: MType): Variable
is
expect(n_mpropdef.n_signature.n_type != null)
do
var actual_block = n_mpropdef.n_block
# never happen. If it's the case the problem is in the contract facet building
assert actual_block isa ABlockExpr
var return_expr = actual_block.n_expr.last.as(AReturnExpr)
var returned_expr = return_expr.n_expr
# The return node has no returned expression
assert returned_expr != null
var return_var = new Variable("result")
# Creating a new variable to keep the old return of the method
var assign_result = v.ast_builder.make_var_assign(return_var, returned_expr)
# Remove the actual return
actual_block.n_expr.pop
# Set the return type
return_var.declared_type = ret_type
# Adding the reading expr of result variable to the block
actual_block.add assign_result
# Expr to read the result variable
var read_result = v.ast_builder.make_var_read(return_var, ret_type)
# Definition of the new return
return_expr = v.ast_builder.make_return(read_result)
actual_block.add return_expr
return return_var
end
src/contracts.nit:474,2--506,4