Z3
 
Loading...
Searching...
No Matches
Constructor.java
Go to the documentation of this file.
1
18package com.microsoft.z3;
19
20import java.lang.ref.ReferenceQueue;
21
25public class Constructor<R> extends Z3Object {
26 private final int n;
27
28 Constructor(Context ctx, int n, long nativeObj) {
29 super(ctx, nativeObj);
30 this.n = n;
31 }
32
39 public int getNumFields()
40 {
41 return n;
42 }
43
50 {
51 Native.LongPtr constructor = new Native.LongPtr();
52 Native.LongPtr tester = new Native.LongPtr();
53 long[] accessors = new long[n];
54 Native.queryConstructor(getContext().nCtx(), getNativeObject(), n, constructor, tester, accessors);
55 return new FuncDecl<>(getContext(), constructor.value);
56 }
57
64 {
65 Native.LongPtr constructor = new Native.LongPtr();
66 Native.LongPtr tester = new Native.LongPtr();
67 long[] accessors = new long[n];
68 Native.queryConstructor(getContext().nCtx(), getNativeObject(), n, constructor, tester, accessors);
69 return new FuncDecl<>(getContext(), tester.value);
70 }
71
78 {
79 Native.LongPtr constructor = new Native.LongPtr();
80 Native.LongPtr tester = new Native.LongPtr();
81 long[] accessors = new long[n];
82 Native.queryConstructor(getContext().nCtx(), getNativeObject(), n, constructor, tester, accessors);
83 FuncDecl<?>[] t = new FuncDecl[n];
84 for (int i = 0; i < n; i++)
85 t[i] = new FuncDecl<>(getContext(), accessors[i]);
86 return t;
87 }
88
89 @Override
90 void incRef() {
91 // Datatype constructors are not reference counted.
92 }
93
94 @Override
95 void addToReferenceQueue() {
96 getContext().getReferenceQueue().storeReference(this, ConstructorRef::new);
97 }
98
99 static <R> Constructor<R> of(Context ctx, Symbol name, Symbol recognizer,
100 Symbol[] fieldNames, Sort[] sorts, int[] sortRefs) {
101 int n = AST.arrayLength(fieldNames);
102
103 if (n != AST.arrayLength(sorts))
104 throw new Z3Exception(
105 "Number of field names does not match number of sorts");
106 if (sortRefs != null && sortRefs.length != n)
107 throw new Z3Exception(
108 "Number of field names does not match number of sort refs");
109
110 if (sortRefs == null)
111 sortRefs = new int[n];
112
113 long nativeObj = Native.mkConstructor(ctx.nCtx(), name.getNativeObject(),
114 recognizer.getNativeObject(), n, Symbol.arrayToNative(fieldNames),
115 Sort.arrayToNative(sorts), sortRefs);
116 return new Constructor<>(ctx, n, nativeObj);
117
118 }
119
120 private static class ConstructorRef extends Z3ReferenceQueue.Reference<Constructor<?>> {
121
122 private ConstructorRef(Constructor<?> referent, ReferenceQueue<Z3Object> q) {
123 super(referent, q);
124 }
125
126 @Override
127 void decRef(Context ctx, long z3Obj) {
128 Native.delConstructor(ctx.nCtx(), z3Obj);
129 }
130 }
131}
FuncDecl<?>[] getAccessorDecls()
FuncDecl< DatatypeSort< R > > ConstructorDecl()
FuncDecl< BoolSort > getTesterDecl()