[FIR] Cleanup formatting in DFA code

This commit is contained in:
Dmitriy Novozhilov
2023-10-17 11:24:18 +03:00
committed by Space Team
parent 9a4c28498c
commit dc08e3bf99
2 changed files with 29 additions and 17 deletions
@@ -1384,18 +1384,22 @@ abstract class FirDataFlowAnalyzer(
} }
} }
private fun MutableFlow.addAllStatements(statements: TypeStatements) = private fun MutableFlow.addAllStatements(statements: TypeStatements) {
statements.values.forEach { addTypeStatement(it) } statements.values.forEach { addTypeStatement(it) }
}
private fun MutableFlow.addAllConditionally(condition: OperationStatement, statements: TypeStatements) = private fun MutableFlow.addAllConditionally(condition: OperationStatement, statements: TypeStatements) {
statements.values.forEach { addImplication(condition implies it) } statements.values.forEach { addImplication(condition implies it) }
}
private fun MutableFlow.addAllConditionally(condition: OperationStatement, from: Flow) = private fun MutableFlow.addAllConditionally(condition: OperationStatement, from: Flow) {
from.knownVariables.forEach { from.knownVariables.forEach {
// Only add the statement if this variable is not aliasing another in `this` (but it could be aliasing in `from`). // Only add the statement if this variable is not aliasing another in `this` (but it could be aliasing in `from`).
if (unwrapVariable(it) == it) addImplication(condition implies (from.getTypeStatement(it) ?: return@forEach)) if (unwrapVariable(it) == it) addImplication(condition implies (from.getTypeStatement(it) ?: return@forEach))
} }
}
private fun MutableFlow.commitOperationStatement(statement: OperationStatement) = private fun MutableFlow.commitOperationStatement(statement: OperationStatement) {
addAllStatements(logicSystem.approveOperationStatement(this, statement, removeApprovedOrImpossible = true)) addAllStatements(logicSystem.approveOperationStatement(this, statement, removeApprovedOrImpossible = true))
}
} }
@@ -17,8 +17,9 @@ abstract class LogicSystem(private val context: ConeInferenceContext) {
abstract val variableStorage: VariableStorageImpl abstract val variableStorage: VariableStorageImpl
protected open fun ConeKotlinType.isAcceptableForSmartcast(): Boolean = protected open fun ConeKotlinType.isAcceptableForSmartcast(): Boolean {
!isNullableNothing return !isNullableNothing
}
fun joinFlow(flows: Collection<PersistentFlow>, union: Boolean): MutableFlow { fun joinFlow(flows: Collection<PersistentFlow>, union: Boolean): MutableFlow {
when (flows.size) { when (flows.size) {
@@ -62,8 +63,8 @@ abstract class LogicSystem(private val context: ConeInferenceContext) {
fun addImplication(flow: MutableFlow, implication: Implication) { fun addImplication(flow: MutableFlow, implication: Implication) {
val effect = implication.effect val effect = implication.effect
if (effect == implication.condition) return if (effect == implication.condition) return
if (effect is TypeStatement && (effect.isEmpty || if (effect is TypeStatement &&
flow.approvedTypeStatements[effect.variable]?.exactType?.containsAll(effect.exactType) == true) (effect.isEmpty || flow.approvedTypeStatements[effect.variable]?.exactType?.containsAll(effect.exactType) == true)
) return ) return
val variable = implication.condition.variable val variable = implication.condition.variable
flow.logicStatements[variable] = flow.logicStatements[variable]?.add(implication) ?: persistentListOf(implication) flow.logicStatements[variable] = flow.logicStatements[variable]?.add(implication) ?: persistentListOf(implication)
@@ -88,14 +89,17 @@ abstract class LogicSystem(private val context: ConeInferenceContext) {
}.build() }.build()
} }
fun approveOperationStatement(flow: PersistentFlow, statement: OperationStatement): TypeStatements = fun approveOperationStatement(flow: PersistentFlow, statement: OperationStatement): TypeStatements {
approveOperationStatement(flow.logicStatements, statement, removeApprovedOrImpossible = false) return approveOperationStatement(flow.logicStatements, statement, removeApprovedOrImpossible = false)
}
fun approveOperationStatement( fun approveOperationStatement(
flow: MutableFlow, flow: MutableFlow,
statement: OperationStatement, statement: OperationStatement,
removeApprovedOrImpossible: Boolean, removeApprovedOrImpossible: Boolean,
): TypeStatements = approveOperationStatement(flow.logicStatements, statement, removeApprovedOrImpossible) ): TypeStatements {
return approveOperationStatement(flow.logicStatements, statement, removeApprovedOrImpossible)
}
fun recordNewAssignment(flow: MutableFlow, variable: RealVariable, index: Int) { fun recordNewAssignment(flow: MutableFlow, variable: RealVariable, index: Int) {
flow.replaceVariable(variable, null) flow.replaceVariable(variable, null)
@@ -294,8 +298,9 @@ abstract class LogicSystem(private val context: ConeInferenceContext) {
exactType += other.exactType exactType += other.exactType
} }
fun and(a: TypeStatement?, b: TypeStatement): TypeStatement = fun and(a: TypeStatement?, b: TypeStatement): TypeStatement {
a?.toMutable()?.apply { this += b } ?: b return a?.toMutable()?.apply { this += b } ?: b
}
fun and(statements: Collection<TypeStatement>): TypeStatement? { fun and(statements: Collection<TypeStatement>): TypeStatement? {
when (statements.size) { when (statements.size) {
@@ -372,13 +377,14 @@ private fun MutableMap<DataFlowVariable, PersistentList<Implication>>.replaceVar
} }
} }
private inline fun <T> PersistentList<T>.replaceAll(block: (T) -> T): PersistentList<T> = private inline fun <T> PersistentList<T>.replaceAll(block: (T) -> T): PersistentList<T> {
mutate { result -> return mutate { result ->
val it = result.listIterator() val it = result.listIterator()
while (it.hasNext()) { while (it.hasNext()) {
it.set(block(it.next())) it.set(block(it.next()))
} }
} }
}
private fun Implication.replaceVariable(from: RealVariable, to: RealVariable): Implication = when { private fun Implication.replaceVariable(from: RealVariable, to: RealVariable): Implication = when {
condition.variable == from -> Implication(condition.copy(variable = to), effect.replaceVariable(from, to)) condition.variable == from -> Implication(condition.copy(variable = to), effect.replaceVariable(from, to))
@@ -386,9 +392,11 @@ private fun Implication.replaceVariable(from: RealVariable, to: RealVariable): I
else -> this else -> this
} }
private fun Statement.replaceVariable(from: RealVariable, to: RealVariable): Statement = private fun Statement.replaceVariable(from: RealVariable, to: RealVariable): Statement {
if (variable != from) this else when (this) { if (variable != from) return this
return when (this) {
is OperationStatement -> copy(variable = to) is OperationStatement -> copy(variable = to)
is PersistentTypeStatement -> copy(variable = to) is PersistentTypeStatement -> copy(variable = to)
is MutableTypeStatement -> MutableTypeStatement(to, exactType) is MutableTypeStatement -> MutableTypeStatement(to, exactType)
} }
}