Z3
 
Loading...
Searching...
No Matches
AST.java
Go to the documentation of this file.
1
18package com.microsoft.z3;
19
20import com.microsoft.z3.enumerations.Z3_ast_kind;
21
22import java.lang.ref.ReferenceQueue;
23
27public class AST extends Z3Object implements Comparable<AST>
28{
34 @Override
35 public boolean equals(Object o)
36 {
37 if (o == this) return true;
38 if (!(o instanceof AST)) return false;
39 AST casted = (AST) o;
40
41 return
42 (getContext().nCtx() == casted.getContext().nCtx()) &&
43 (Native.isEqAst(getContext().nCtx(), getNativeObject(), casted.getNativeObject()));
44 }
45
54 @Override
55 public int compareTo(AST other)
56 {
57 if (other == null) {
58 return 1;
59 }
60 return Integer.compare(getId(), other.getId());
61 }
62
68 @Override
69 public int hashCode()
70 {
71 return Native.getAstHash(getContext().nCtx(), getNativeObject());
72 }
73
78 public int getId()
79 {
80 return Native.getAstId(getContext().nCtx(), getNativeObject());
81 }
82
88 public int getDepth()
89 {
90 return Native.getDepth(getContext().nCtx(), getNativeObject());
91 }
92
101 {
102 if (getContext() == ctx) {
103 return this;
104 } else {
105 return create(ctx, Native.translate(getContext().nCtx(), getNativeObject(), ctx.nCtx()));
106 }
107 }
108
114 {
115 return Z3_ast_kind.fromInt(Native.getAstKind(getContext().nCtx(),
116 getNativeObject()));
117 }
118
124 public boolean isExpr()
125 {
126 switch (getASTKind())
127 {
128 case Z3_APP_AST:
129 case Z3_NUMERAL_AST:
131 case Z3_VAR_AST:
132 return true;
133 default:
134 return false;
135 }
136 }
137
143 public boolean isApp()
144 {
145 return this.getASTKind() == Z3_ast_kind.Z3_APP_AST;
146 }
147
153 public boolean isVar()
154 {
155 return this.getASTKind() == Z3_ast_kind.Z3_VAR_AST;
156 }
157
163 public boolean isQuantifier()
164 {
165 return this.getASTKind() == Z3_ast_kind.Z3_QUANTIFIER_AST;
166 }
167
171 public boolean isSort()
172 {
173 return this.getASTKind() == Z3_ast_kind.Z3_SORT_AST;
174 }
175
179 public boolean isFuncDecl()
180 {
181 return this.getASTKind() == Z3_ast_kind.Z3_FUNC_DECL_AST;
182 }
183
187 @Override
188 public String toString() {
189 return Native.astToString(getContext().nCtx(), getNativeObject());
190 }
191
195 public String getSExpr()
196 {
197 return Native.astToString(getContext().nCtx(), getNativeObject());
198 }
199
200 AST(Context ctx, long obj) {
201 super(ctx, obj);
202 }
203
204 @Override
205 void incRef() {
206 Native.incRef(getContext().nCtx(), getNativeObject());
207 }
208
209 @Override
210 void addToReferenceQueue() {
211 getContext().getReferenceQueue().storeReference(this, ASTRef::new);
212 }
213
214 static AST create(Context ctx, long obj)
215 {
216 switch (Z3_ast_kind.fromInt(Native.getAstKind(ctx.nCtx(), obj)))
217 {
218 case Z3_FUNC_DECL_AST:
219 return new FuncDecl<>(ctx, obj);
221 // a quantifier AST is a lambda iff it is neither a forall nor an exists.
222 boolean isLambda = !Native.isQuantifierExists(ctx.nCtx(), obj) && !Native.isQuantifierForall(ctx.nCtx(), obj);
223 if (isLambda) {
224 return new Lambda(ctx, obj);
225 } else {
226 return new Quantifier(ctx, obj);
227 }
228 case Z3_SORT_AST:
229 return Sort.create(ctx, obj);
230 case Z3_APP_AST:
231 case Z3_NUMERAL_AST:
232 case Z3_VAR_AST:
233 return Expr.create(ctx, obj);
234 default:
235 throw new Z3Exception("Unknown AST kind");
236 }
237 }
238
239 private static class ASTRef extends Z3ReferenceQueue.Reference<AST> {
240
241 private ASTRef(AST referent, ReferenceQueue<Z3Object> q) {
242 super(referent, q);
243 }
244
245 @Override
246 void decRef(Context ctx, long z3Obj) {
247 Native.decRef(ctx.nCtx(), z3Obj);
248 }
249 }
250}
String getSExpr()
Definition AST.java:195
boolean isFuncDecl()
Definition AST.java:179
boolean isQuantifier()
Definition AST.java:163
boolean equals(Object o)
Definition AST.java:35
int compareTo(AST other)
Definition AST.java:55
boolean isApp()
Definition AST.java:143
boolean isSort()
Definition AST.java:171
AST translate(Context ctx)
Definition AST.java:100
boolean isExpr()
Definition AST.java:124
boolean isVar()
Definition AST.java:153
String toString()
Definition AST.java:188
Z3_ast_kind getASTKind()
Definition AST.java:113
Z3_ast_kind
The different kinds of Z3 AST (abstract syntax trees). That is, terms, formulas and types.
Definition z3_api.h:142
@ Z3_APP_AST
Definition z3_api.h:144
@ Z3_VAR_AST
Definition z3_api.h:145
@ Z3_SORT_AST
Definition z3_api.h:147
@ Z3_NUMERAL_AST
Definition z3_api.h:143
@ Z3_FUNC_DECL_AST
Definition z3_api.h:148
@ Z3_QUANTIFIER_AST
Definition z3_api.h:146