All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.io.ksmt.solver.z3.KZ3DeclInternalizer.kt Maven / Gradle / Ivy

package io.ksmt.solver.z3

import com.microsoft.z3.Native
import io.ksmt.decl.KConstDecl
import io.ksmt.decl.KDecl
import io.ksmt.decl.KDeclVisitor
import io.ksmt.decl.KFuncDecl
import io.ksmt.solver.util.KExprLongInternalizerBase.Companion.NOT_INTERNALIZED
import io.ksmt.sort.KSort

open class KZ3DeclInternalizer(
    private val z3Ctx: KZ3Context,
    private val sortInternalizer: KZ3SortInternalizer
) : KDeclVisitor {
    private val nCtx = z3Ctx.nCtx
    private var lastInternalizedDecl: Long = NOT_INTERNALIZED

    override fun  visit(decl: KFuncDecl) {
        val domainSorts = decl.argSorts.let { argSorts ->
            LongArray(argSorts.size) { sortInternalizer.internalizeZ3Sort(argSorts[it]) }
        }

        val rangeSort = sortInternalizer.internalizeZ3Sort(decl.sort)
        val nameSymbol = Native.mkStringSymbol(nCtx, decl.name)

        lastInternalizedDecl = Native.mkFuncDecl(
            nCtx,
            nameSymbol,
            domainSorts.size,
            domainSorts,
            rangeSort
        )
    }

    override fun  visit(decl: KConstDecl) {
        val declSort = sortInternalizer.internalizeZ3Sort(decl.sort)
        val nameSymbol = Native.mkStringSymbol(nCtx, decl.name)

        lastInternalizedDecl = Native.mkFuncDecl(
            nCtx,
            nameSymbol,
            0,
            null,
            declSort
        )
    }

    fun internalizeZ3Decl(decl: KDecl<*>): Long = z3Ctx.internalizeDecl(decl) {
        decl.accept(this@KZ3DeclInternalizer)
        lastInternalizedDecl
    }
}