3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-08-02 09:20:22 +00:00
z3/src/api/dotnet/ASTVector.cs
Nikolaj Bjorner edeeded4ea
remove DecRefQueue, use Z3_enable_concurrent_dec_ref (#6332)
The notion of reference counted contexts never worked.
The reference count to a context only ends up being 0 if the GC kicks in and disposes the various z3 objects. A call to Dispose on Context should free up all resources associated with that context. In exchange none of the resources are allowed any other operation than DecRef. The invocations of DecRef are protected by a lock and test on the context that the native pointer associated with the context is non-zero. Dispose sets the native pointer to zero.

Z3_enable_concurrent_dec_ref ensures that:

- calls to decref are thread safe. Other threads can operate on the context without interference.

The Z3_context ensures that
- z3objects allocated, but not disposed during the lifetime of Z3_context are freed when Z3_context is deleted (it triggers a debug warning, but this is now benign).
2022-09-11 18:59:00 -07:00

252 lines
7.3 KiB
C#

/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
ASTVector.cs
Abstract:
Z3 Managed API: AST Vectors
Author:
Christoph Wintersteiger (cwinter) 2012-03-21
Notes:
--*/
using System.Diagnostics;
using System;
namespace Microsoft.Z3
{
/// <summary>
/// Vectors of ASTs.
/// </summary>
public class ASTVector : Z3Object
{
/// <summary>
/// The size of the vector
/// </summary>
public uint Size
{
get { return Native.Z3_ast_vector_size(Context.nCtx, NativeObject); }
}
/// <summary>
/// Retrieves the i-th object in the vector.
/// </summary>
/// <remarks>May throw an IndexOutOfBoundsException when <paramref name="i"/> is out of range.</remarks>
/// <param name="i">Index</param>
/// <returns>An AST</returns>
public AST this[uint i]
{
get
{
return new AST(Context, Native.Z3_ast_vector_get(Context.nCtx, NativeObject, i));
}
set
{
Debug.Assert(value != null);
Native.Z3_ast_vector_set(Context.nCtx, NativeObject, i, value.NativeObject);
}
}
/// <summary>
/// Resize the vector to <paramref name="newSize"/>.
/// </summary>
/// <param name="newSize">The new size of the vector.</param>
public void Resize(uint newSize)
{
Native.Z3_ast_vector_resize(Context.nCtx, NativeObject, newSize);
}
/// <summary>
/// Add the AST <paramref name="a"/> to the back of the vector. The size
/// is increased by 1.
/// </summary>
/// <param name="a">An AST</param>
public void Push(AST a)
{
Debug.Assert(a != null);
Native.Z3_ast_vector_push(Context.nCtx, NativeObject, a.NativeObject);
}
/// <summary>
/// Translates all ASTs in the vector to <paramref name="ctx"/>.
/// </summary>
/// <param name="ctx">A context</param>
/// <returns>A new ASTVector</returns>
public ASTVector Translate(Context ctx)
{
Debug.Assert(ctx != null);
return new ASTVector(Context, Native.Z3_ast_vector_translate(Context.nCtx, NativeObject, ctx.nCtx));
}
/// <summary>
/// Retrieves a string representation of the vector.
/// </summary>
public override string ToString()
{
return Native.Z3_ast_vector_to_string(Context.nCtx, NativeObject);
}
/// <summary>
/// Translates an AST vector into an AST[]
/// </summary>
public AST[] ToArray()
{
uint n = Size;
AST[] res = new AST[n];
for (uint i = 0; i < n; i++)
res[i] = AST.Create(this.Context, this[i].NativeObject);
return res;
}
/// <summary>
/// Translates an ASTVector into an Expr[]
/// </summary>
public Expr[] ToExprArray()
{
uint n = Size;
Expr[] res = new Expr[n];
for (uint i = 0; i < n; i++)
res[i] = Expr.Create(this.Context, this[i].NativeObject);
return res;
}
/// <summary>
/// Translates an ASTVector into a BoolExpr[]
/// </summary>
public BoolExpr[] ToBoolExprArray()
{
uint n = Size;
BoolExpr[] res = new BoolExpr[n];
for (uint i = 0; i < n; i++)
res[i] = (BoolExpr) Expr.Create(this.Context, this[i].NativeObject);
return res;
}
/// <summary>
/// Translates an ASTVector into a BitVecExpr[]
/// </summary>
public BitVecExpr[] ToBitVecExprArray()
{
uint n = Size;
BitVecExpr[] res = new BitVecExpr[n];
for (uint i = 0; i < n; i++)
res[i] = (BitVecExpr)Expr.Create(this.Context, this[i].NativeObject);
return res;
}
/// <summary>
/// Translates an ASTVector into a ArithExpr[]
/// </summary>
public ArithExpr[] ToArithExprArray()
{
uint n = Size;
ArithExpr[] res = new ArithExpr[n];
for (uint i = 0; i < n; i++)
res[i] = (ArithExpr)Expr.Create(this.Context, this[i].NativeObject);
return res;
}
/// <summary>
/// Translates an ASTVector into a ArrayExpr[]
/// </summary>
public ArrayExpr[] ToArrayExprArray()
{
uint n = Size;
ArrayExpr[] res = new ArrayExpr[n];
for (uint i = 0; i < n; i++)
res[i] = (ArrayExpr)Expr.Create(this.Context, this[i].NativeObject);
return res;
}
/// <summary>
/// Translates an ASTVector into a DatatypeExpr[]
/// </summary>
public DatatypeExpr[] ToDatatypeExprArray()
{
uint n = Size;
DatatypeExpr[] res = new DatatypeExpr[n];
for (uint i = 0; i < n; i++)
res[i] = (DatatypeExpr)Expr.Create(this.Context, this[i].NativeObject);
return res;
}
/// <summary>
/// Translates an ASTVector into a FPExpr[]
/// </summary>
public FPExpr[] ToFPExprArray()
{
uint n = Size;
FPExpr[] res = new FPExpr[n];
for (uint i = 0; i < n; i++)
res[i] = (FPExpr)Expr.Create(this.Context, this[i].NativeObject);
return res;
}
/// <summary>
/// Translates an ASTVector into a FPRMExpr[]
/// </summary>
public FPRMExpr[] ToFPRMExprArray()
{
uint n = Size;
FPRMExpr[] res = new FPRMExpr[n];
for (uint i = 0; i < n; i++)
res[i] = (FPRMExpr)Expr.Create(this.Context, this[i].NativeObject);
return res;
}
/// <summary>
/// Translates an ASTVector into a IntExpr[]
/// </summary>
public IntExpr[] ToIntExprArray()
{
uint n = Size;
IntExpr[] res = new IntExpr[n];
for (uint i = 0; i < n; i++)
res[i] = (IntExpr)Expr.Create(this.Context, this[i].NativeObject);
return res;
}
/// <summary>
/// Translates an ASTVector into a RealExpr[]
/// </summary>
public RealExpr[] ToRealExprArray()
{
uint n = Size;
RealExpr[] res = new RealExpr[n];
for (uint i = 0; i < n; i++)
res[i] = (RealExpr)Expr.Create(this.Context, this[i].NativeObject);
return res;
}
#region Internal
internal ASTVector(Context ctx, IntPtr obj) : base(ctx, obj) { Debug.Assert(ctx != null); }
internal ASTVector(Context ctx) : base(ctx, Native.Z3_mk_ast_vector(ctx.nCtx)) { Debug.Assert(ctx != null); }
internal override void IncRef(IntPtr o)
{
Native.Z3_ast_vector_inc_ref(Context.nCtx, o);
}
internal override void DecRef(IntPtr o)
{
lock (Context)
{
if (Context.nCtx != IntPtr.Zero)
Native.Z3_ast_vector_dec_ref(Context.nCtx, o);
}
}
#endregion
}
}