1use crate::contract::{BinOp, ContractExpr, ContractPlace, ContractProjection, PlaceBase, RelOp, UnaryOp};
4use crate::util::mir_utils;
5use crate::vm::state::VmValue;
6use crate::vm::VmExecutor;
7use rustc_middle::{mir::Local, ty::TyKind};
8use z3::ast::{Ast, Bool, Int};
9
10impl<'z3, 'tcx> VmExecutor<'z3, 'tcx> {
11 pub(crate) fn bound_contract_place_value(&self, place: &ContractPlace) -> Option<VmValue<'z3, 'tcx>> {
13 let local = place.base.to_local();
14 if place.projections.is_empty() { return self.local_value(local).cloned() }
15 let mut fields = Vec::new();
16 for projection in &place.projections {
17 match projection {
18 ContractProjection::Field { index } => fields.push(*index),
19 ContractProjection::Downcast { .. } | ContractProjection::ForEach => {
20 return if fields.is_empty() { self.local_value(local).cloned() } else { self.field_value(local, &fields).cloned() };
21 }
22 }
23 }
24 self.field_value(local, &fields).cloned()
25 }
26
27 fn eval_contract_fact_tree(&self, expr: &ContractExpr<'tcx>) -> Option<Int<'z3>> {
28 match expr {
29 ContractExpr::Const(value) => Some(self.u128_term(*value)),
30 ContractExpr::Binary { op, lhs, rhs } => {
31 let left = self.eval_contract_fact_tree(lhs)?;
32 let right = self.eval_contract_fact_tree(rhs)?;
33 match op {
34 BinOp::Add | BinOp::Sub | BinOp::Mul => self.contract_binary_term(*op, &left, &right),
35 BinOp::Div => Some(left.div(&right)),
36 BinOp::Rem => Some(left.rem(&right)),
37 _ => None,
38 }
39 }
40 ContractExpr::Unary { op, expr } => {
41 let value = self.eval_contract_fact_tree(expr)?;
42 Some(self.contract_unary_term(*op, &value))
43 }
44 _ => self.eval_contract_expr_simple_leaf(expr),
45 }
46 }
47
48 pub(crate) fn eval_contract_expr_simple(&self, expr: &ContractExpr<'tcx>) -> Option<Int<'z3>> {
49 self.eval_contract_fact_tree(expr)
50 }
51
52 fn eval_contract_expr_simple_leaf(&self, expr: &ContractExpr<'tcx>) -> Option<Int<'z3>> {
53 match expr {
54 ContractExpr::SizeOf(ty) => Some(self.size_sym_read(*ty)),
55 ContractExpr::Place(cp) => {
56 let path = cp.plain_field_path()?;
57 if let Some(value) = self.bound_contract_place_value(cp)
58 {
59 return Some(value.z3_term);
60 }
61 if path.is_empty() {
62 return None;
63 }
64 let local = cp.base.to_local();
65 let base_val = self.local_value(local)?;
66 let alloc_id = base_val.provenance_alloc_id()?;
67 let view_ty = mir_utils::pointee_ty(base_val.ty).unwrap_or(base_val.ty);
68 self.content(alloc_id)
69 .values
70 .get(&(view_ty, path))
71 .map(|value| value.z3_term.clone())
72 }
73 ContractExpr::Len(inner, _) => {
74 let val = self.eval_contract_expr_simple_value(inner)?;
75 if self.is_local_adt_receiver(&val) {
76 return None;
77 }
78 if let Some(term) = self.try_simple_iter_len(&val) {
79 return Some(term);
80 }
81 if let ContractExpr::Place(cp) = &**inner
82 && let Some(path) = cp.plain_field_path()
83 {
84 let local = cp.base.to_local();
85 if let Some(ty) = self.field_type_at(local, &path)
86 && let Some(term) = self.try_struct_nn_len_field(local, &path, ty)
87 {
88 return Some(term);
89 }
90 }
91 self.len_from_value(&val)
92 }
93 ContractExpr::MethodCall {
94 receiver,
95 method,
96 args,
97 } => {
98 let value = self.eval_contract_expr_simple_value(receiver)?;
99 let local = match receiver.as_ref() {
100 ContractExpr::Place(cp) => Some(cp.base.to_local()),
101 _ => None,
102 };
103 let args = args.iter().map(|arg| self.eval_contract_fact_tree(arg)).collect::<Option<Vec<_>>>()?;
104 self.eval_contract_method(*method, value, local, args)
105 }
106 ContractExpr::ConstParam { index, .. } => self.const_param_term(*index),
107 _ => None,
108 }
109 }
110
111 fn eval_contract_expr_simple_value(&self, expr: &ContractExpr<'tcx>) -> Option<VmValue<'z3, 'tcx>> {
112 match expr {
113 ContractExpr::Place(cp) => match cp.base {
114 PlaceBase::Local(n) => self.local_value(Local::from_usize(n)).cloned(),
115 _ => None,
116 },
117 _ => None,
118 }
119 }
120
121 fn try_simple_iter_len(&self, arg_val: &VmValue<'z3, 'tcx>) -> Option<Int<'z3>> {
122 if !self.is_iter_ref(arg_val) {
123 return None;
124 }
125 let local = Local::from_usize(1);
126 let ptr = self.field_value(local, &[0])?;
127 let end = self.field_value(local, &[1])?;
128 self.iter_len_from_ptrs(ptr, end)
129 }
130
131 pub(crate) fn is_local_adt_receiver(&self, value: &VmValue<'z3, 'tcx>) -> bool {
132 let ty = mir_utils::pointee_ty(value.ty).unwrap_or(value.ty);
133 matches!(ty.kind(), TyKind::Adt(adt, _) if adt.did().as_local().is_some())
134 }
135
136 pub(crate) fn contract_binary_term(
137 &self,
138 op: BinOp,
139 left: &Int<'z3>,
140 right: &Int<'z3>,
141 ) -> Option<Int<'z3>> {
142 match op {
143 BinOp::Add => Some(Int::add(self.z3_ctx, &[left, right])),
144 BinOp::Sub => Some(Int::sub(self.z3_ctx, &[left, right])),
145 BinOp::Mul => Some(Int::mul(self.z3_ctx, &[left, right])),
146 _ => None,
147 }
148 }
149
150 pub(crate) fn contract_unary_term(&self, op: UnaryOp, value: &Int<'z3>) -> Int<'z3> {
151 let zero = Int::from_u64(self.z3_ctx, 0);
152 match op {
153 UnaryOp::Not => value._eq(&zero).ite(&Int::from_u64(self.z3_ctx, 1), &zero),
154 UnaryOp::Neg => Int::sub(self.z3_ctx, &[&zero, value]),
155 }
156 }
157
158 pub(crate) fn contract_relop_bool(
159 &self,
160 op: RelOp,
161 left: &Int<'z3>,
162 right: &Int<'z3>,
163 ) -> Bool<'z3> {
164 match op {
165 RelOp::Eq => left._eq(right),
166 RelOp::Ne => left._eq(right).not(),
167 RelOp::Le => left.le(right),
168 RelOp::Lt => left.lt(right),
169 RelOp::Ge => left.ge(right),
170 RelOp::Gt => left.gt(right),
171 }
172 }
173}