[FIR] Clean data flow context before body resolve of file

This commit is contained in:
Dmitriy Novozhilov
2020-05-19 12:01:49 +03:00
parent 8df635a4e0
commit dc57d307f3
5 changed files with 28 additions and 0 deletions
@@ -36,6 +36,13 @@ class DataFlowAnalyzerContext<FLOW : Flow>(
val flowOnNodes: MutableMap<CFGNode<*>, FLOW>,
val variablesForWhenConditions: MutableMap<WhenBranchConditionExitNode, DataFlowVariable>
) {
fun reset() {
graphBuilder.reset()
variableStorage.reset()
flowOnNodes.clear()
variablesForWhenConditions.clear()
}
companion object {
fun <FLOW : Flow> empty(session: FirSession) =
DataFlowAnalyzerContext<FLOW>(
@@ -15,6 +15,7 @@ abstract class Stack<T> {
abstract fun top(): T
abstract fun pop(): T
abstract fun push(value: T)
abstract fun reset()
}
fun <T> stackOf(vararg values: T): Stack<T> = StackImpl(*values)
@@ -33,6 +34,9 @@ private class StackImpl<T>(vararg values: T) : Stack<T>() {
}
override val size: Int get() = stack.size
override fun reset() {
stack.clear()
}
}
class NodeStorage<T : FirElement, N : CFGNode<T>> : Stack<N>(){
@@ -55,6 +59,11 @@ class NodeStorage<T : FirElement, N : CFGNode<T>> : Stack<N>(){
operator fun get(key: T): N? {
return map[key]
}
override fun reset() {
stack.reset()
map.clear()
}
}
class SymbolBasedNodeStorage<T, N : CFGNode<T>> : Stack<N>() where T : FirElement {
@@ -77,4 +86,9 @@ class SymbolBasedNodeStorage<T, N : CFGNode<T>> : Stack<N>() where T : FirElemen
operator fun get(key: FirBasedSymbol<*>): N? {
return map[key]
}
override fun reset() {
stack.reset()
map.clear()
}
}
@@ -941,6 +941,8 @@ class ControlFlowGraphBuilder {
fun reset() {
exitsOfAnonymousFunctions.clear()
exitsFromCompletedPostponedAnonymousFunctions.clear()
lexicalScopes.reset()
lexicalScopes.push(stackOf())
}
fun dropSubgraphFromCall(call: FirFunctionCall) {
@@ -229,6 +229,10 @@ abstract class FirAbstractBodyResolveTransformer(phase: FirResolvePhase) : FirAb
towerDataContextForAnonymousFunctions.clear()
}
fun cleanDataFlowContext() {
dataFlowAnalyzerContext.reset()
}
private inline fun updateLastScope(transform: FirLocalScope.() -> FirLocalScope) {
val lastScope = towerDataContext.localScopes.lastOrNull() ?: return
replaceTowerDataContext(towerDataContext.setLastLocalScope(lastScope.transform()))
@@ -46,6 +46,7 @@ open class FirBodyResolveTransformer(
override fun transformFile(file: FirFile, data: ResolutionMode): CompositeTransformResult<FirFile> {
checkSessionConsistency(file)
context.cleanContextForAnonymousFunction()
context.cleanDataFlowContext()
@OptIn(PrivateForInline::class)
context.file = file
packageFqName = file.packageFqName