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
90 public AST translate(Context ctx)
91 {
92 if (getContext() == ctx) {
93 return this;
94 } else {
95 return create(ctx, Native.translate(getContext().nCtx(), getNativeObject(), ctx.nCtx()));
96 }
97 }
98
104 {
105 return Z3_ast_kind.fromInt(Native.getAstKind(getContext().nCtx(),
106 getNativeObject()));
107 }
108
114 public boolean isExpr()
115 {
116 switch (getASTKind())
117 {
118 case Z3_APP_AST:
119 case Z3_NUMERAL_AST:
121 case Z3_VAR_AST:
122 return true;
123 default:
124 return false;
125 }
126 }
127
133 public boolean isApp()
134 {
135 return this.getASTKind() == Z3_ast_kind.Z3_APP_AST;
136 }
137
143 public boolean isVar()
144 {
145 return this.getASTKind() == Z3_ast_kind.Z3_VAR_AST;
146 }
147
153 public boolean isQuantifier()
154 {
155 return this.getASTKind() == Z3_ast_kind.Z3_QUANTIFIER_AST;
156 }
157
161 public boolean isSort()
162 {
163 return this.getASTKind() == Z3_ast_kind.Z3_SORT_AST;
164 }
165
169 public boolean isFuncDecl()
170 {
171 return this.getASTKind() == Z3_ast_kind.Z3_FUNC_DECL_AST;
172 }
173
177 @Override
178 public String toString() {
179 return Native.astToString(getContext().nCtx(), getNativeObject());
180 }
181
185 public String getSExpr()
186 {
187 return Native.astToString(getContext().nCtx(), getNativeObject());
188 }
189
190 AST(Context ctx, long obj) {
191 super(ctx, obj);
192 }
193
194 @Override
195 void incRef() {
196 Native.incRef(getContext().nCtx(), getNativeObject());
197 }
198
199 @Override
200 void addToReferenceQueue() {
201 getContext().getReferenceQueue().storeReference(this, ASTRef::new);
202 }
203
204 static AST create(Context ctx, long obj)
205 {
206 switch (Z3_ast_kind.fromInt(Native.getAstKind(ctx.nCtx(), obj)))
207 {
208 case Z3_FUNC_DECL_AST:
209 return new FuncDecl<>(ctx, obj);
211 // a quantifier AST is a lambda iff it is neither a forall nor an exists.
212 boolean isLambda = !Native.isQuantifierExists(ctx.nCtx(), obj) && !Native.isQuantifierForall(ctx.nCtx(), obj);
213 if (isLambda) {
214 return new Lambda(ctx, obj);
215 } else {
216 return new Quantifier(ctx, obj);
217 }
218 case Z3_SORT_AST:
219 return Sort.create(ctx, obj);
220 case Z3_APP_AST:
221 case Z3_NUMERAL_AST:
222 case Z3_VAR_AST:
223 return Expr.create(ctx, obj);
224 default:
225 throw new Z3Exception("Unknown AST kind");
226 }
227 }
228
229 private static class ASTRef extends Z3ReferenceQueue.Reference<AST> {
230
231 private ASTRef(AST referent, ReferenceQueue<Z3Object> q) {
232 super(referent, q);
233 }
234
235 @Override
236 void decRef(Context ctx, long z3Obj) {
237 Native.decRef(ctx.nCtx(), z3Obj);
238 }
239 }
240}
String getSExpr()
Definition AST.java:185
boolean isFuncDecl()
Definition AST.java:169
boolean isQuantifier()
Definition AST.java:153
boolean equals(Object o)
Definition AST.java:35
int compareTo(AST other)
Definition AST.java:55
boolean isApp()
Definition AST.java:133
boolean isSort()
Definition AST.java:161
AST translate(Context ctx)
Definition AST.java:90
boolean isExpr()
Definition AST.java:114
boolean isVar()
Definition AST.java:143
String toString()
Definition AST.java:178
Z3_ast_kind getASTKind()
Definition AST.java:103
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