|
| 1 | +package org.utbot.engine.simplificators |
| 2 | + |
| 3 | +import kotlinx.collections.immutable.PersistentList |
| 4 | +import kotlinx.collections.immutable.PersistentMap |
| 5 | +import kotlinx.collections.immutable.PersistentSet |
| 6 | +import kotlinx.collections.immutable.mutate |
| 7 | +import kotlinx.collections.immutable.toPersistentMap |
| 8 | +import kotlinx.collections.immutable.toPersistentSet |
| 9 | +import org.utbot.engine.Concrete |
| 10 | +import org.utbot.engine.InstanceFieldReadOperation |
| 11 | +import org.utbot.engine.MemoryChunkDescriptor |
| 12 | +import org.utbot.engine.MemoryUpdate |
| 13 | +import org.utbot.engine.MockInfoEnriched |
| 14 | +import org.utbot.engine.ObjectValue |
| 15 | +import org.utbot.engine.StaticFieldMemoryUpdateInfo |
| 16 | +import org.utbot.engine.UtMockInfo |
| 17 | +import org.utbot.engine.UtNamedStore |
| 18 | +import org.utbot.engine.pc.Simplificator |
| 19 | +import org.utbot.engine.pc.UtAddrExpression |
| 20 | +import org.utbot.framework.plugin.api.ClassId |
| 21 | +import org.utbot.framework.plugin.api.FieldId |
| 22 | +import soot.ArrayType |
| 23 | + |
| 24 | +typealias StoresType = PersistentList<UtNamedStore> |
| 25 | +typealias TouchedChunkDescriptorsType = PersistentSet<MemoryChunkDescriptor> |
| 26 | +typealias ConcreteType = PersistentMap<UtAddrExpression, Concrete> |
| 27 | +typealias MockInfosType = PersistentList<MockInfoEnriched> |
| 28 | +typealias StaticInstanceStorageType = PersistentMap<ClassId, ObjectValue> |
| 29 | +typealias InitializedStaticFieldsType = PersistentSet<FieldId> |
| 30 | +typealias StaticFieldsUpdatesType = PersistentList<StaticFieldMemoryUpdateInfo> |
| 31 | +typealias MeaningfulStaticFieldsType = PersistentSet<FieldId> |
| 32 | +typealias AddrToArrayTypeType = PersistentMap<UtAddrExpression, ArrayType> |
| 33 | +typealias AddrToMockInfoType = PersistentMap<UtAddrExpression, UtMockInfo> |
| 34 | +typealias VisitedValuesType = PersistentList<UtAddrExpression> |
| 35 | +typealias TouchedAddressesType = PersistentList<UtAddrExpression> |
| 36 | +typealias ClassIdToClearStaticsType = ClassId? |
| 37 | +typealias InstanceFieldReadsType = PersistentSet<InstanceFieldReadOperation> |
| 38 | +typealias SpeculativelyNotNullAddressesType = PersistentList<UtAddrExpression> |
| 39 | +typealias SymbolicEnumValuesType = PersistentList<ObjectValue> |
| 40 | + |
| 41 | +context(Simplificator) |
| 42 | +class MemoryUpdateSimplificator : CachingSimplificatorAdapter<MemoryUpdate>() { |
| 43 | + override fun simplifyImpl(expression: MemoryUpdate): MemoryUpdate { |
| 44 | + val stores = simplifyStores(expression.stores) |
| 45 | + val touchedChunkDescriptors = simplifyTocuhedChunkDescriptors(expression.touchedChunkDescriptors) |
| 46 | + val concrete = simplifyConcrete(expression.concrete) |
| 47 | + val mockInfos = simplifyMockInfos(expression.mockInfos) |
| 48 | + val staticInstanceStorage = simplifyStaticInstanceStorage(expression.staticInstanceStorage) |
| 49 | + val initializedStaticFields = simplifyInitializedStaticFields(expression.initializedStaticFields) |
| 50 | + val staticFieldsUpdates = simplifyStaticFieldsUpdates(expression.staticFieldsUpdates) |
| 51 | + val meaningfulStaticFields = simplifyMeaningfulStaticFields(expression.meaningfulStaticFields) |
| 52 | + val addrToArrayType = simplifyAddrToArrayType(expression.addrToArrayType) |
| 53 | + val addrToMockInfo = simplifyAddToMockInfo(expression.addrToMockInfo) |
| 54 | + val visitedValues = simplifyVisitedValues(expression.visitedValues) |
| 55 | + val touchedAddresses = simplifyTouchedAddresses(expression.touchedAddresses) |
| 56 | + val classIdToClearStatics = simplifyClassIdToClearStatics(expression.classIdToClearStatics) |
| 57 | + val instanceFieldReads = simplifyInstanceFieldReads(expression.instanceFieldReads) |
| 58 | + val speculativelyNotNullAddresses = |
| 59 | + simplifySpeculativelyNotNullAddresses(expression.speculativelyNotNullAddresses) |
| 60 | + val symbolicEnumValues = simplifyEnumValues(expression.symbolicEnumValues) |
| 61 | + return MemoryUpdate( |
| 62 | + stores, |
| 63 | + touchedChunkDescriptors, |
| 64 | + concrete, |
| 65 | + mockInfos, |
| 66 | + staticInstanceStorage, |
| 67 | + initializedStaticFields, |
| 68 | + staticFieldsUpdates, |
| 69 | + meaningfulStaticFields, |
| 70 | + addrToArrayType, |
| 71 | + addrToMockInfo, |
| 72 | + visitedValues, |
| 73 | + touchedAddresses, |
| 74 | + classIdToClearStatics, |
| 75 | + instanceFieldReads, |
| 76 | + speculativelyNotNullAddresses, |
| 77 | + symbolicEnumValues |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + private fun simplifyStores(stores: StoresType): StoresType = |
| 82 | + stores |
| 83 | + .mutate { prevStores -> |
| 84 | + prevStores.replaceAll { store -> |
| 85 | + store.copy( |
| 86 | + index = store.index.accept(this@Simplificator), |
| 87 | + value = store.value.accept(this@Simplificator) |
| 88 | + ) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private fun simplifyTocuhedChunkDescriptors(touchedChunkDescriptors: TouchedChunkDescriptorsType): TouchedChunkDescriptorsType = |
| 93 | + touchedChunkDescriptors |
| 94 | + |
| 95 | + private fun simplifyConcrete(concrete: ConcreteType): ConcreteType = |
| 96 | + concrete |
| 97 | + .mapKeys { (k, _) -> k.accept(this@Simplificator) as UtAddrExpression } |
| 98 | + .toPersistentMap() |
| 99 | + |
| 100 | + private fun simplifyMockInfos(mockInfos: MockInfosType): MockInfosType = |
| 101 | + mockInfos.mutate { prevMockInfos -> |
| 102 | + prevMockInfos.replaceAll { |
| 103 | + simplifyMockInfoEnriched(it) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + private fun simplifyStaticInstanceStorage(staticInstanceStorage: StaticInstanceStorageType): StaticInstanceStorageType = |
| 109 | + staticInstanceStorage.mutate { prevStorage -> |
| 110 | + prevStorage.replaceAll { _, v -> simplifySymbolicValue(v) as ObjectValue } |
| 111 | + } |
| 112 | + |
| 113 | + private fun simplifyInitializedStaticFields(initializedStaticFields: InitializedStaticFieldsType): InitializedStaticFieldsType = |
| 114 | + initializedStaticFields |
| 115 | + |
| 116 | + private fun simplifyStaticFieldsUpdates(staticFieldsUpdates: StaticFieldsUpdatesType): StaticFieldsUpdatesType = |
| 117 | + staticFieldsUpdates.mutate { prevUpdates -> |
| 118 | + prevUpdates.replaceAll { staticFieldMemoryUpdateInfo(it) } |
| 119 | + } |
| 120 | + |
| 121 | + private fun simplifyMeaningfulStaticFields(meaningfulStaticFields: MeaningfulStaticFieldsType): MeaningfulStaticFieldsType = |
| 122 | + meaningfulStaticFields |
| 123 | + |
| 124 | + |
| 125 | + private fun simplifyAddrToArrayType(addrToArrayType: AddrToArrayTypeType): AddrToArrayTypeType = |
| 126 | + addrToArrayType |
| 127 | + .mapKeys { (k, _) -> k.accept(this@Simplificator) as UtAddrExpression } |
| 128 | + .toPersistentMap() |
| 129 | + |
| 130 | + |
| 131 | + private fun simplifyAddToMockInfo(addrToMockInfo: AddrToMockInfoType): AddrToMockInfoType = |
| 132 | + addrToMockInfo |
| 133 | + .mapKeys { (k, _) -> k.accept(this@Simplificator) as UtAddrExpression } |
| 134 | + .toPersistentMap() |
| 135 | + |
| 136 | + private fun simplifyVisitedValues(visitedValues: VisitedValuesType): VisitedValuesType = |
| 137 | + visitedValues.mutate { prevValues -> |
| 138 | + prevValues.replaceAll { it.accept(this@Simplificator) as UtAddrExpression } |
| 139 | + } |
| 140 | + |
| 141 | + private fun simplifyTouchedAddresses(touchedAddresses: TouchedAddressesType): TouchedAddressesType = |
| 142 | + touchedAddresses.mutate { prevAddresses -> |
| 143 | + prevAddresses.replaceAll { it.accept(this@Simplificator) as UtAddrExpression } |
| 144 | + } |
| 145 | + |
| 146 | + private fun simplifyClassIdToClearStatics(classIdToClearStatics: ClassIdToClearStaticsType): ClassIdToClearStaticsType = |
| 147 | + classIdToClearStatics |
| 148 | + |
| 149 | + private fun simplifyInstanceFieldReads(instanceFieldReads: InstanceFieldReadsType): InstanceFieldReadsType = |
| 150 | + instanceFieldReads |
| 151 | + .map { it.copy(addr = it.addr.accept(this@Simplificator) as UtAddrExpression) } |
| 152 | + .toPersistentSet() |
| 153 | + |
| 154 | + private fun simplifySpeculativelyNotNullAddresses(speculativelyNotNullAddresses: SpeculativelyNotNullAddressesType): SpeculativelyNotNullAddressesType = |
| 155 | + speculativelyNotNullAddresses.mutate { prevAddresses -> |
| 156 | + prevAddresses.replaceAll { it.accept(this@Simplificator) as UtAddrExpression } |
| 157 | + } |
| 158 | + |
| 159 | + private fun simplifyEnumValues(symbolicEnumValues: SymbolicEnumValuesType): SymbolicEnumValuesType = |
| 160 | + symbolicEnumValues.mutate { values -> |
| 161 | + values.replaceAll { simplifySymbolicValue(it) as ObjectValue } |
| 162 | + } |
| 163 | +} |
0 commit comments