|
1 | 1 | package org.usvm.api |
2 | 2 |
|
3 | 3 | import org.usvm.UBoolExpr |
| 4 | +import org.usvm.UExpr |
4 | 5 | import org.usvm.UHeapRef |
| 6 | +import org.usvm.UNullRef |
| 7 | +import org.usvm.USort |
5 | 8 | import org.usvm.UState |
| 9 | +import org.usvm.constraints.UTypeEvaluator |
| 10 | +import org.usvm.memory.mapWithStaticAsConcrete |
| 11 | +import org.usvm.types.UTypeStream |
| 12 | +import org.usvm.types.singleOrNull |
| 13 | +import org.usvm.uctx |
6 | 14 |
|
| 15 | +fun <Type> UTypeEvaluator<Type>.evalTypeEquals(ref: UHeapRef, type: Type): UBoolExpr = |
| 16 | + with(ref.uctx) { |
| 17 | + mkAnd( |
| 18 | + evalIsSubtype(ref, type), |
| 19 | + evalIsSupertype(ref, type) |
| 20 | + ) |
| 21 | + } |
7 | 22 |
|
8 | | -fun UState<*, *, *, *, *, *>.assume(expr: UBoolExpr) { |
9 | | - pathConstraints += expr |
| 23 | +fun <Type> UState<Type, *, *, *, *, *>.objectTypeEquals( |
| 24 | + lhs: UHeapRef, |
| 25 | + rhs: UHeapRef |
| 26 | +): UBoolExpr = with(lhs.uctx) { |
| 27 | + mapTypeStream( |
| 28 | + ref = lhs, |
| 29 | + onNull = { |
| 30 | + // type(null) = type(null); type(null) <: T /\ T <: type(null) ==> true /\ false |
| 31 | + mapTypeStream(rhs, onNull = { trueExpr }, { _, _ -> falseExpr }) |
| 32 | + }, |
| 33 | + operation = { lhsRef, lhsTypes -> |
| 34 | + mapTypeStream( |
| 35 | + rhs, |
| 36 | + onNull = { falseExpr }, |
| 37 | + operation = { rhsRef, rhsTypes -> |
| 38 | + mkTypeEqualsConstraint(lhsRef, lhsTypes, rhsRef, rhsTypes) |
| 39 | + } |
| 40 | + ) |
| 41 | + } |
| 42 | + ) |
10 | 43 | } |
11 | 44 |
|
12 | | -fun UState<*, *, *, *, *, *>.objectTypeEquals(lhs: UHeapRef, rhs: UHeapRef): UBoolExpr { |
13 | | - TODO("Objects types equality check: $lhs, $rhs") |
| 45 | +fun <Type, R : USort> UState<Type, *, *, *, *, *>.mapTypeStream( |
| 46 | + ref: UHeapRef, |
| 47 | + operation: (UHeapRef, UTypeStream<Type>) -> UExpr<R>? |
| 48 | +): UExpr<R>? = mapTypeStream( |
| 49 | + ref = ref, |
| 50 | + onNull = { return null }, |
| 51 | + operation = { expr, types -> |
| 52 | + operation(expr, types) ?: return null |
| 53 | + } |
| 54 | +) |
| 55 | + |
| 56 | +private fun <Type> UState<Type, *, *, *, *, *>.mkTypeEqualsConstraint( |
| 57 | + lhs: UHeapRef, |
| 58 | + lhsTypes: UTypeStream<Type>, |
| 59 | + rhs: UHeapRef, |
| 60 | + rhsTypes: UTypeStream<Type>, |
| 61 | +): UBoolExpr = with(lhs.uctx) { |
| 62 | + val lhsType = lhsTypes.singleOrNull() |
| 63 | + val rhsType = rhsTypes.singleOrNull() |
| 64 | + |
| 65 | + if (lhsType != null) { |
| 66 | + return if (lhsType == rhsType) { |
| 67 | + trueExpr |
| 68 | + } else { |
| 69 | + memory.types.evalTypeEquals(rhs, lhsType) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (rhsType != null) { |
| 74 | + return memory.types.evalTypeEquals(lhs, rhsType) |
| 75 | + } |
| 76 | + |
| 77 | + // TODO: don't mock type equals |
| 78 | + makeSymbolicPrimitive(boolSort) |
14 | 79 | } |
| 80 | + |
| 81 | +private inline fun <Type, R : USort> UState<Type, *, *, *, *, *>.mapTypeStream( |
| 82 | + ref: UHeapRef, |
| 83 | + onNull: () -> UExpr<R>, |
| 84 | + operation: (UHeapRef, UTypeStream<Type>) -> UExpr<R> |
| 85 | +): UExpr<R> = ref.mapWithStaticAsConcrete( |
| 86 | + ignoreNullRefs = false, |
| 87 | + concreteMapper = { concreteRef -> |
| 88 | + val types = memory.types.getTypeStream(concreteRef) |
| 89 | + operation(concreteRef, types) |
| 90 | + }, |
| 91 | + symbolicMapper = { symbolicRef -> |
| 92 | + if (symbolicRef is UNullRef) { |
| 93 | + onNull() |
| 94 | + } else { |
| 95 | + val types = memory.types.getTypeStream(symbolicRef) |
| 96 | + operation(symbolicRef, types) |
| 97 | + } |
| 98 | + }, |
| 99 | +) |
0 commit comments