FIR DFA: refactor type statements manipulation
This commit is contained in:
committed by
Dmitriy Novozhilov
parent
7ea58adc50
commit
16b9312695
@@ -122,16 +122,22 @@ abstract class LogicSystem<FLOW : Flow>(protected val context: ConeInferenceCont
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun or(statements: Collection<TypeStatement>): MutableTypeStatement {
|
private inline fun manipulateTypeStatements(
|
||||||
|
statements: Collection<TypeStatement>,
|
||||||
|
op: (Collection<Set<ConeKotlinType>>) -> MutableSet<ConeKotlinType>
|
||||||
|
): MutableTypeStatement {
|
||||||
require(statements.isNotEmpty())
|
require(statements.isNotEmpty())
|
||||||
statements.singleOrNull()?.let { return it as MutableTypeStatement }
|
statements.singleOrNull()?.let { return it as MutableTypeStatement }
|
||||||
val variable = statements.first().variable
|
val variable = statements.first().variable
|
||||||
assert(statements.all { it.variable == variable })
|
assert(statements.all { it.variable == variable })
|
||||||
val exactType = orForTypes(statements.map { it.exactType })
|
val exactType = op.invoke(statements.map { it.exactType })
|
||||||
val exactNotType = orForTypes(statements.map { it.exactNotType })
|
val exactNotType = op.invoke(statements.map { it.exactNotType })
|
||||||
return MutableTypeStatement(variable, exactType, exactNotType)
|
return MutableTypeStatement(variable, exactType, exactNotType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected fun or(statements: Collection<TypeStatement>): MutableTypeStatement =
|
||||||
|
manipulateTypeStatements(statements, ::orForTypes)
|
||||||
|
|
||||||
private fun orForTypes(types: Collection<Set<ConeKotlinType>>): MutableSet<ConeKotlinType> {
|
private fun orForTypes(types: Collection<Set<ConeKotlinType>>): MutableSet<ConeKotlinType> {
|
||||||
if (types.any { it.isEmpty() }) return mutableSetOf()
|
if (types.any { it.isEmpty() }) return mutableSetOf()
|
||||||
val allTypes = types.flatMapTo(mutableSetOf()) { it }
|
val allTypes = types.flatMapTo(mutableSetOf()) { it }
|
||||||
@@ -144,15 +150,8 @@ abstract class LogicSystem<FLOW : Flow>(protected val context: ConeInferenceCont
|
|||||||
return commonTypes
|
return commonTypes
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun and(statements: Collection<TypeStatement>): MutableTypeStatement {
|
protected fun and(statements: Collection<TypeStatement>): MutableTypeStatement =
|
||||||
require(statements.isNotEmpty())
|
manipulateTypeStatements(statements, ::andForTypes)
|
||||||
statements.singleOrNull()?.let { return it as MutableTypeStatement }
|
|
||||||
val variable = statements.first().variable
|
|
||||||
assert(statements.all { it.variable == variable })
|
|
||||||
val exactType = andForTypes(statements.map { it.exactType })
|
|
||||||
val exactNotType = andForTypes(statements.map { it.exactNotType })
|
|
||||||
return MutableTypeStatement(variable, exactType, exactNotType)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun andForTypes(types: Collection<Set<ConeKotlinType>>): MutableSet<ConeKotlinType> {
|
private fun andForTypes(types: Collection<Set<ConeKotlinType>>): MutableSet<ConeKotlinType> {
|
||||||
return types.flatMapTo(mutableSetOf()) { it }
|
return types.flatMapTo(mutableSetOf()) { it }
|
||||||
|
|||||||
Reference in New Issue
Block a user