1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use ast::Ast;
use std::convert::TryInto;
use std::ffi::CStr;
use std::fmt;
use z3_sys::*;
use Context;
use Pattern;
use Z3_MUTEX;
impl<'ctx> Pattern<'ctx> {
pub fn new(ctx: &'ctx Context, terms: &[&dyn Ast]) -> Pattern<'ctx> {
assert!(!terms.is_empty());
assert!(terms.iter().all(|t| t.get_ctx().z3_ctx == ctx.z3_ctx));
let terms: Vec<_> = terms.iter().map(|t| t.get_z3_ast()).collect();
Pattern {
ctx,
z3_pattern: unsafe {
let guard = Z3_MUTEX.lock().unwrap();
let p = Z3_mk_pattern(
ctx.z3_ctx,
terms.len().try_into().unwrap(),
terms.as_ptr() as *const Z3_ast,
);
Z3_inc_ref(ctx.z3_ctx, p as Z3_ast);
p
},
}
}
}
impl<'ctx> fmt::Debug for Pattern<'ctx> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let p = unsafe { Z3_pattern_to_string(self.ctx.z3_ctx, self.z3_pattern) };
if p.is_null() {
return Result::Err(fmt::Error);
}
match unsafe { CStr::from_ptr(p) }.to_str() {
Ok(s) => write!(f, "{}", s),
Err(_) => Result::Err(fmt::Error),
}
}
}
impl<'ctx> fmt::Display for Pattern<'ctx> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
<Self as fmt::Debug>::fmt(self, f)
}
}
impl<'ctx> Drop for Pattern<'ctx> {
fn drop(&mut self) {
unsafe {
Z3_dec_ref(self.ctx.z3_ctx, self.z3_pattern as Z3_ast);
}
}
}