Skip to main content

z3_sys/
types.rs

1//! Customized FFI types for the Z3 API.
2//!
3//! Custom FFI types, rather than the bindgden defaults allow us to
4//! express properties of the Z3 API not formally expressed in the C ABI:
5//!
6//! * Z3, with a couple exceptions for output parameters, requires that all
7//!   pointers to Z3-allocated objects passed as parameters be non-null.
8//! * Any Z3 function which returns a pointer will return a null pointer if
9//!   Z3 encounters an internal error, or if the invocation was invalid in
10//!   some way (e.g. adding two BVs of different lengths)
11//!
12//! To ensure the Rust bindings uphold both these properties, we use the following
13//! strategy:
14//! * We define our own pointer types to opaque Z3 structures that as `NonNull` pointers.
15//! * We rewrite the bindgen-generated functions to return Option<Z3_type> to explicitly
16//!   indicate nullability.
17//!
18//! These two changes force us to ensure pointers are valid before calling Z3 and to check
19//! the returned pointer results from Z3 before we use them.
20//!
21//! This module contains the customized Opaque structure definitions and their associated
22//! NonNull pointer aliases.
23use core::ptr::NonNull;
24
25use crate::Z3_error_code;
26
27#[doc(hidden)]
28#[repr(C)]
29#[derive(Debug, Copy, Clone)]
30pub struct _Z3_symbol {
31    _unused: [u8; 0],
32}
33/// Lisp-like symbol used to name types, constants, and functions.
34/// A symbol can be created using string or integers.
35///
36/// # See also:
37///
38/// - [`crate::Z3_get_symbol_int`]
39/// - [`crate::Z3_get_symbol_kind`]
40/// - [`crate::Z3_get_symbol_string`]
41/// - [`crate::Z3_mk_int_symbol`]
42/// - [`crate::Z3_mk_string_symbol`]
43pub type Z3_symbol = NonNull<_Z3_symbol>;
44
45#[doc(hidden)]
46#[repr(C)]
47#[derive(Debug, Copy, Clone)]
48pub struct _Z3_literals {
49    _unused: [u8; 0],
50}
51
52pub type Z3_literals = NonNull<_Z3_literals>;
53
54#[doc(hidden)]
55#[repr(C)]
56#[derive(Debug, Copy, Clone)]
57pub struct _Z3_config {
58    _unused: [u8; 0],
59}
60/// Configuration object used to initialize logical contexts.
61pub type Z3_config = NonNull<_Z3_config>;
62
63#[doc(hidden)]
64#[repr(C)]
65#[derive(Debug, Copy, Clone)]
66pub struct _Z3_context {
67    _unused: [u8; 0],
68}
69/// Manager of all other Z3 objects, global configuration options, etc.
70pub type Z3_context = NonNull<_Z3_context>;
71
72#[doc(hidden)]
73#[repr(C)]
74#[derive(Debug, Copy, Clone)]
75pub struct _Z3_sort {
76    _unused: [u8; 0],
77}
78/// Kind of AST used to represent types.
79pub type Z3_sort = NonNull<_Z3_sort>;
80
81#[doc(hidden)]
82#[repr(C)]
83#[derive(Debug, Copy, Clone)]
84pub struct _Z3_func_decl {
85    _unused: [u8; 0],
86}
87/// Kind of AST used to represent function symbols.
88pub type Z3_func_decl = NonNull<_Z3_func_decl>;
89
90#[doc(hidden)]
91#[repr(C)]
92#[derive(Debug, Copy, Clone)]
93pub struct _Z3_ast {
94    _unused: [u8; 0],
95}
96/// Abstract Syntax Tree node. That is, the data structure used in Z3
97/// to represent terms, formulas, and types.
98pub type Z3_ast = NonNull<_Z3_ast>;
99
100#[doc(hidden)]
101#[repr(C)]
102#[derive(Debug, Copy, Clone)]
103pub struct _Z3_app {
104    _unused: [u8; 0],
105}
106/// Kind of AST used to represent function applications.
107pub type Z3_app = NonNull<_Z3_app>;
108
109#[doc(hidden)]
110#[repr(C)]
111#[derive(Debug, Copy, Clone)]
112pub struct _Z3_pattern {
113    _unused: [u8; 0],
114}
115/// Kind of AST used to represent pattern and multi-patterns used
116/// to guide quantifier instantiation.
117pub type Z3_pattern = NonNull<_Z3_pattern>;
118
119#[doc(hidden)]
120#[repr(C)]
121#[derive(Debug, Copy, Clone)]
122pub struct _Z3_model {
123    _unused: [u8; 0],
124}
125/// Model for the constraints inserted into the logical context.
126pub type Z3_model = NonNull<_Z3_model>;
127
128#[doc(hidden)]
129#[repr(C)]
130#[derive(Debug, Copy, Clone)]
131pub struct _Z3_constructor {
132    _unused: [u8; 0],
133}
134/// Type constructor for a (recursive) datatype.
135pub type Z3_constructor = NonNull<_Z3_constructor>;
136
137#[doc(hidden)]
138#[repr(C)]
139#[derive(Debug, Copy, Clone)]
140pub struct _Z3_constructor_list {
141    _unused: [u8; 0],
142}
143/// List of constructors for a (recursive) datatype.
144pub type Z3_constructor_list = NonNull<_Z3_constructor_list>;
145
146#[doc(hidden)]
147#[repr(C)]
148#[derive(Debug, Copy, Clone)]
149pub struct _Z3_params {
150    _unused: [u8; 0],
151}
152/// Parameter set used to configure many components such as:
153/// simplifiers, tactics, solvers, etc.
154pub type Z3_params = NonNull<_Z3_params>;
155
156#[doc(hidden)]
157#[repr(C)]
158#[derive(Debug, Copy, Clone)]
159pub struct _Z3_param_descrs {
160    _unused: [u8; 0],
161}
162/// Provides a collection of parameter names, their types,
163/// default values and documentation strings. Solvers, tactics,
164/// and other objects accept different collection of parameters.
165pub type Z3_param_descrs = NonNull<_Z3_param_descrs>;
166
167#[doc(hidden)]
168#[repr(C)]
169#[derive(Debug, Copy, Clone)]
170pub struct _Z3_parser_context {
171    _unused: [u8; 0],
172}
173/// Context for incrementally parsing SMTLIB2 strings.
174pub type Z3_parser_context = NonNull<_Z3_parser_context>;
175
176#[doc(hidden)]
177#[repr(C)]
178#[derive(Debug, Copy, Clone)]
179pub struct _Z3_goal {
180    _unused: [u8; 0],
181}
182/// Set of formulas that can be solved and/or transformed using
183/// tactics and solvers.
184pub type Z3_goal = NonNull<_Z3_goal>;
185
186#[doc(hidden)]
187#[repr(C)]
188#[derive(Debug, Copy, Clone)]
189pub struct _Z3_tactic {
190    _unused: [u8; 0],
191}
192/// Basic building block for creating custom solvers for specific
193/// problem domains.
194pub type Z3_tactic = NonNull<_Z3_tactic>;
195
196#[doc(hidden)]
197#[repr(C)]
198#[derive(Debug, Copy, Clone)]
199pub struct _Z3_simplifier {
200    _unused: [u8; 0],
201}
202/// Simplifier object.
203pub type Z3_simplifier = NonNull<_Z3_simplifier>;
204
205#[doc(hidden)]
206#[repr(C)]
207#[derive(Debug, Copy, Clone)]
208pub struct _Z3_probe {
209    _unused: [u8; 0],
210}
211/// Function/predicate used to inspect a goal and collect information
212/// that may be used to decide which solver and/or preprocessing step
213/// will be used.
214pub type Z3_probe = NonNull<_Z3_probe>;
215
216#[doc(hidden)]
217#[repr(C)]
218#[derive(Debug, Copy, Clone)]
219pub struct _Z3_stats {
220    _unused: [u8; 0],
221}
222/// Statistical data for a solver.
223pub type Z3_stats = NonNull<_Z3_stats>;
224
225#[doc(hidden)]
226#[repr(C)]
227#[derive(Debug, Copy, Clone)]
228pub struct _Z3_solver {
229    _unused: [u8; 0],
230}
231/// (Incremental) solver, possibly specialized by a particular
232/// tactic or logic.
233pub type Z3_solver = NonNull<_Z3_solver>;
234
235#[doc(hidden)]
236#[repr(C)]
237#[derive(Debug, Copy, Clone)]
238pub struct _Z3_solver_callback {
239    _unused: [u8; 0],
240}
241/// Callback object for user-defined solver propagation.
242pub type Z3_solver_callback = NonNull<_Z3_solver_callback>;
243
244#[doc(hidden)]
245#[repr(C)]
246#[derive(Debug, Copy, Clone)]
247pub struct _Z3_ast_vector {
248    _unused: [u8; 0],
249}
250/// Vector of [`Z3_ast`] objects.
251pub type Z3_ast_vector = NonNull<_Z3_ast_vector>;
252
253#[doc(hidden)]
254#[repr(C)]
255#[derive(Debug, Copy, Clone)]
256pub struct _Z3_ast_map {
257    _unused: [u8; 0],
258}
259/// Mapping from [`Z3_ast`] to [`Z3_ast`] objects.
260pub type Z3_ast_map = NonNull<_Z3_ast_map>;
261
262#[doc(hidden)]
263#[repr(C)]
264#[derive(Debug, Copy, Clone)]
265pub struct _Z3_apply_result {
266    _unused: [u8; 0],
267}
268/// Collection of subgoals resulting from applying of a tactic
269/// to a goal.
270pub type Z3_apply_result = NonNull<_Z3_apply_result>;
271
272#[doc(hidden)]
273#[repr(C)]
274#[derive(Debug, Copy, Clone)]
275pub struct _Z3_func_interp {
276    _unused: [u8; 0],
277}
278/// Interpretation of a function in a model.
279pub type Z3_func_interp = NonNull<_Z3_func_interp>;
280
281#[doc(hidden)]
282#[repr(C)]
283#[derive(Debug, Copy, Clone)]
284pub struct _Z3_func_entry {
285    _unused: [u8; 0],
286}
287/// Representation of the value of a [`Z3_func_interp`]
288/// at a particular point.
289pub type Z3_func_entry = NonNull<_Z3_func_entry>;
290
291#[doc(hidden)]
292#[repr(C)]
293#[derive(Debug, Copy, Clone)]
294pub struct _Z3_fixedpoint {
295    _unused: [u8; 0],
296}
297/// Context for the recursive predicate solver.
298pub type Z3_fixedpoint = NonNull<_Z3_fixedpoint>;
299
300#[doc(hidden)]
301#[repr(C)]
302#[derive(Debug, Copy, Clone)]
303pub struct _Z3_optimize {
304    _unused: [u8; 0],
305}
306/// Context for solving optimization queries.
307pub type Z3_optimize = NonNull<_Z3_optimize>;
308
309#[doc(hidden)]
310#[repr(C)]
311#[derive(Debug, Copy, Clone)]
312pub struct _Z3_rcf_num {
313    _unused: [u8; 0],
314}
315pub type Z3_rcf_num = NonNull<_Z3_rcf_num>;
316
317/// Z3 string type. It is just an alias for `const char *`.
318pub type Z3_string = *const ::core::ffi::c_char;
319
320pub type Z3_string_ptr = *mut Z3_string;
321
322pub const Z3_L_FALSE: Z3_lbool = -1;
323pub const Z3_L_UNDEF: Z3_lbool = 0;
324pub const Z3_L_TRUE: Z3_lbool = 1;
325
326/// Lifted Boolean type: `false`, `undefined`, `true`.
327pub type Z3_lbool = i32;
328
329/// Z3 custom error handler (See [`crate::Z3_set_error_handler`]).
330pub type Z3_error_handler =
331    ::core::option::Option<unsafe extern "C" fn(c: Z3_context, e: Z3_error_code)>;
332
333/// The following utilities allows adding user-defined domains.
334pub type Z3_fixedpoint_reduce_assign_callback_fptr = ::core::option::Option<
335    unsafe extern "C" fn(
336        arg1: *mut ::core::ffi::c_void,
337        arg2: Z3_func_decl,
338        arg3: ::core::ffi::c_uint,
339        arg4: *const Z3_ast,
340        arg5: ::core::ffi::c_uint,
341        arg6: *const Z3_ast,
342    ),
343>;
344pub type Z3_fixedpoint_reduce_app_callback_fptr = ::core::option::Option<
345    unsafe extern "C" fn(
346        arg1: *mut ::core::ffi::c_void,
347        arg2: Z3_func_decl,
348        arg3: ::core::ffi::c_uint,
349        arg4: *const Z3_ast,
350        arg5: *mut Z3_ast,
351    ),
352>;
353
354pub type Z3_fixedpoint_new_lemma_eh = ::core::option::Option<
355    unsafe extern "C" fn(
356        state: *mut ::core::ffi::c_void,
357        lemma: Z3_ast,
358        level: ::core::ffi::c_uint,
359    ),
360>;
361pub type Z3_fixedpoint_predecessor_eh =
362    ::core::option::Option<unsafe extern "C" fn(state: *mut ::core::ffi::c_void)>;
363pub type Z3_fixedpoint_unfold_eh =
364    ::core::option::Option<unsafe extern "C" fn(state: *mut ::core::ffi::c_void)>;
365
366/// Pointer to a C string (same underlying type as [`Z3_string`] but used as output).
367pub type Z3_char_ptr = *const ::core::ffi::c_char;
368
369/// Callback invoked when a new scope is pushed during solver propagation.
370pub type Z3_push_eh = ::core::option::Option<
371    unsafe extern "C" fn(ctx: *mut ::core::ffi::c_void, cb: Z3_solver_callback),
372>;
373
374/// Callback invoked when scopes are popped during solver propagation.
375pub type Z3_pop_eh = ::core::option::Option<
376    unsafe extern "C" fn(
377        ctx: *mut ::core::ffi::c_void,
378        cb: Z3_solver_callback,
379        num_scopes: ::core::ffi::c_uint,
380    ),
381>;
382
383/// Callback invoked to create a fresh user context for a new solver thread.
384pub type Z3_fresh_eh = ::core::option::Option<
385    unsafe extern "C" fn(
386        ctx: *mut ::core::ffi::c_void,
387        new_context: Z3_context,
388    ) -> *mut ::core::ffi::c_void,
389>;
390
391/// Callback invoked when a variable is fixed during solver propagation.
392pub type Z3_fixed_eh = ::core::option::Option<
393    unsafe extern "C" fn(
394        ctx: *mut ::core::ffi::c_void,
395        cb: Z3_solver_callback,
396        t: Z3_ast,
397        value: Z3_ast,
398    ),
399>;
400
401/// Callback invoked when all remaining assignments are final during solver propagation.
402pub type Z3_final_eh = ::core::option::Option<
403    unsafe extern "C" fn(ctx: *mut ::core::ffi::c_void, cb: Z3_solver_callback),
404>;
405
406/// Callback invoked when two terms are equated during solver propagation.
407pub type Z3_eq_eh = ::core::option::Option<
408    unsafe extern "C" fn(
409        ctx: *mut ::core::ffi::c_void,
410        cb: Z3_solver_callback,
411        s: Z3_ast,
412        t: Z3_ast,
413    ),
414>;
415
416/// Callback invoked when a new term is created during solver propagation.
417pub type Z3_created_eh = ::core::option::Option<
418    unsafe extern "C" fn(ctx: *mut ::core::ffi::c_void, cb: Z3_solver_callback, t: Z3_ast),
419>;
420
421/// Callback invoked when a clause is learned by the solver.
422pub type Z3_on_clause_eh = ::core::option::Option<
423    unsafe extern "C" fn(
424        ctx: *mut ::core::ffi::c_void,
425        proof_hint: Z3_ast,
426        n: ::core::ffi::c_uint,
427        deps: *const ::core::ffi::c_uint,
428        literals: Z3_ast_vector,
429    ),
430>;
431
432/// Callback invoked when the solver makes a decision during propagation.
433pub type Z3_decide_eh = ::core::option::Option<
434    unsafe extern "C" fn(
435        ctx: *mut ::core::ffi::c_void,
436        cb: Z3_solver_callback,
437        t: Z3_ast,
438        idx: ::core::ffi::c_uint,
439        phase: bool,
440    ),
441>;
442
443/// Callback invoked on binding during solver propagation; returns whether to keep the binding.
444pub type Z3_on_binding_eh = ::core::option::Option<
445    unsafe extern "C" fn(
446        ctx: *mut ::core::ffi::c_void,
447        cb: Z3_solver_callback,
448        q: Z3_ast,
449        inst: Z3_ast,
450    ) -> bool,
451>;
452
453/// Callback invoked when an optimization model is found.
454pub type Z3_model_eh = ::core::option::Option<unsafe extern "C" fn(ctx: *mut ::core::ffi::c_void)>;