[FIR] Replace ControlFlowGraphNodeBuilder with extensions on ControlFlowGraphBuilder

This commit is contained in:
Dmitriy Novozhilov
2019-09-04 19:10:34 +03:00
parent 15ff86fc50
commit beaab19eb9
2 changed files with 103 additions and 110 deletions
@@ -15,9 +15,9 @@ import org.jetbrains.kotlin.fir.resolve.dfa.*
import org.jetbrains.kotlin.fir.resolve.transformers.resultType import org.jetbrains.kotlin.fir.resolve.transformers.resultType
import org.jetbrains.kotlin.fir.types.isNothing import org.jetbrains.kotlin.fir.types.isNothing
class ControlFlowGraphBuilder : ControlFlowGraphNodeBuilder() { class ControlFlowGraphBuilder {
private val graphs: Stack<ControlFlowGraph> = stackOf(ControlFlowGraph("<DUMP_GRAPH_FOR_ENUMS>")) private val graphs: Stack<ControlFlowGraph> = stackOf(ControlFlowGraph("<DUMP_GRAPH_FOR_ENUMS>"))
override val graph: ControlFlowGraph get() = graphs.top() val graph: ControlFlowGraph get() = graphs.top()
private val lexicalScopes: Stack<Stack<CFGNode<*>>> = stackOf(stackOf()) private val lexicalScopes: Stack<Stack<CFGNode<*>>> = stackOf(stackOf())
private val lastNodes: Stack<CFGNode<*>> get() = lexicalScopes.top() private val lastNodes: Stack<CFGNode<*>> get() = lexicalScopes.top()
@@ -49,7 +49,8 @@ class ControlFlowGraphBuilder : ControlFlowGraphNodeBuilder() {
popCallback = { exitNodes.pop() } popCallback = { exitNodes.pop() }
) )
override var levelCounter: Int = 0 var levelCounter: Int = 0
private set
fun isTopLevel(): Boolean = graphs.size == 1 fun isTopLevel(): Boolean = graphs.size == 1
@@ -10,160 +10,152 @@ import org.jetbrains.kotlin.fir.declarations.FirFunction
import org.jetbrains.kotlin.fir.declarations.FirProperty import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.expressions.*
abstract class ControlFlowGraphNodeBuilder { fun ControlFlowGraphBuilder.createStubNode(): StubNode = StubNode(graph, levelCounter)
protected abstract val graph: ControlFlowGraph
protected abstract var levelCounter: Int
protected fun createStubNode(): StubNode = StubNode( fun ControlFlowGraphBuilder.createLoopExitNode(fir: FirLoop): LoopExitNode = LoopExitNode(graph, fir, levelCounter)
graph,
levelCounter
)
protected fun createLoopExitNode(fir: FirLoop): LoopExitNode = LoopExitNode(graph, fir, levelCounter) fun ControlFlowGraphBuilder.createLoopEnterNode(fir: FirLoop): LoopEnterNode = LoopEnterNode(graph, fir, levelCounter)
protected fun createLoopEnterNode(fir: FirLoop): LoopEnterNode = LoopEnterNode(graph, fir, levelCounter) fun ControlFlowGraphBuilder.createInitBlockExitNode(fir: FirAnonymousInitializer): InitBlockExitNode =
InitBlockExitNode(graph, fir, levelCounter)
protected fun createInitBlockExitNode(fir: FirAnonymousInitializer): InitBlockExitNode = fun ControlFlowGraphBuilder.createInitBlockEnterNode(fir: FirAnonymousInitializer): InitBlockEnterNode =
InitBlockExitNode(graph, fir, levelCounter) InitBlockEnterNode(graph, fir, levelCounter)
protected fun createInitBlockEnterNode(fir: FirAnonymousInitializer): InitBlockEnterNode = fun ControlFlowGraphBuilder.createTypeOperatorCallNode(fir: FirTypeOperatorCall): TypeOperatorCallNode =
InitBlockEnterNode(graph, fir, levelCounter) TypeOperatorCallNode(graph, fir, levelCounter)
protected fun createTypeOperatorCallNode(fir: FirTypeOperatorCall): TypeOperatorCallNode = fun ControlFlowGraphBuilder.createOperatorCallNode(fir: FirOperatorCall): OperatorCallNode =
TypeOperatorCallNode(graph, fir, levelCounter) OperatorCallNode(graph, fir, levelCounter)
protected fun createOperatorCallNode(fir: FirOperatorCall): OperatorCallNode = fun ControlFlowGraphBuilder.createWhenBranchConditionExitNode(fir: FirWhenBranch): WhenBranchConditionExitNode =
OperatorCallNode(graph, fir, levelCounter) WhenBranchConditionExitNode(graph, fir, levelCounter)
protected fun createWhenBranchConditionExitNode(fir: FirWhenBranch): WhenBranchConditionExitNode = fun ControlFlowGraphBuilder.createJumpNode(fir: FirJump<*>): JumpNode =
WhenBranchConditionExitNode(graph, fir, levelCounter) JumpNode(graph, fir, levelCounter)
protected fun createJumpNode(fir: FirJump<*>): JumpNode = fun ControlFlowGraphBuilder.createQualifiedAccessNode(fir: FirQualifiedAccessExpression): QualifiedAccessNode =
JumpNode(graph, fir, levelCounter) QualifiedAccessNode(graph, fir, levelCounter)
protected fun createQualifiedAccessNode(fir: FirQualifiedAccessExpression): QualifiedAccessNode = fun ControlFlowGraphBuilder.createBlockEnterNode(fir: FirBlock): BlockEnterNode = BlockEnterNode(graph, fir, levelCounter)
QualifiedAccessNode(graph, fir, levelCounter)
protected fun createBlockEnterNode(fir: FirBlock): BlockEnterNode = BlockEnterNode(graph, fir, levelCounter) fun ControlFlowGraphBuilder.createBlockExitNode(fir: FirBlock): BlockExitNode = BlockExitNode(graph, fir, levelCounter)
protected fun createBlockExitNode(fir: FirBlock): BlockExitNode = BlockExitNode(graph, fir, levelCounter) fun ControlFlowGraphBuilder.createPropertyInitializerExitNode(fir: FirProperty): PropertyInitializerExitNode =
PropertyInitializerExitNode(graph, fir, levelCounter)
protected fun createPropertyInitializerExitNode(fir: FirProperty): PropertyInitializerExitNode = fun ControlFlowGraphBuilder.createPropertyInitializerEnterNode(fir: FirProperty): PropertyInitializerEnterNode =
PropertyInitializerExitNode(graph, fir, levelCounter) PropertyInitializerEnterNode(graph, fir, levelCounter)
protected fun createPropertyInitializerEnterNode(fir: FirProperty): PropertyInitializerEnterNode = fun ControlFlowGraphBuilder.createFunctionEnterNode(fir: FirFunction<*>, isInPlace: Boolean): FunctionEnterNode =
PropertyInitializerEnterNode(graph, fir, levelCounter) FunctionEnterNode(graph, fir, levelCounter).also {
if (!isInPlace) {
protected fun createFunctionEnterNode(fir: FirFunction<*>, isInPlace: Boolean): FunctionEnterNode = graph.enterNode = it
FunctionEnterNode(graph, fir, levelCounter).also {
if (!isInPlace) {
graph.enterNode = it
}
} }
}
protected fun createFunctionExitNode(fir: FirFunction<*>, isInPlace: Boolean): FunctionExitNode = fun ControlFlowGraphBuilder.createFunctionExitNode(fir: FirFunction<*>, isInPlace: Boolean): FunctionExitNode =
FunctionExitNode(graph, fir, levelCounter).also { FunctionExitNode(graph, fir, levelCounter).also {
if (!isInPlace) { if (!isInPlace) {
graph.exitNode = it graph.exitNode = it
}
} }
}
protected fun createBinaryOrEnterNode(fir: FirBinaryLogicExpression): BinaryOrEnterNode = fun ControlFlowGraphBuilder.createBinaryOrEnterNode(fir: FirBinaryLogicExpression): BinaryOrEnterNode =
BinaryOrEnterNode(graph, fir, levelCounter) BinaryOrEnterNode(graph, fir, levelCounter)
protected fun createBinaryOrExitLeftOperandNode(fir: FirBinaryLogicExpression): BinaryOrExitLeftOperandNode = fun ControlFlowGraphBuilder.createBinaryOrExitLeftOperandNode(fir: FirBinaryLogicExpression): BinaryOrExitLeftOperandNode =
BinaryOrExitLeftOperandNode(graph, fir, levelCounter) BinaryOrExitLeftOperandNode(graph, fir, levelCounter)
protected fun createBinaryOrExitNode(fir: FirBinaryLogicExpression): BinaryOrExitNode = fun ControlFlowGraphBuilder.createBinaryOrExitNode(fir: FirBinaryLogicExpression): BinaryOrExitNode =
BinaryOrExitNode(graph, fir, levelCounter) BinaryOrExitNode(graph, fir, levelCounter)
protected fun createBinaryAndExitNode(fir: FirBinaryLogicExpression): BinaryAndExitNode = fun ControlFlowGraphBuilder.createBinaryAndExitNode(fir: FirBinaryLogicExpression): BinaryAndExitNode =
BinaryAndExitNode(graph, fir, levelCounter) BinaryAndExitNode(graph, fir, levelCounter)
protected fun createBinaryAndEnterNode(fir: FirBinaryLogicExpression): BinaryAndEnterNode = fun ControlFlowGraphBuilder.createBinaryAndEnterNode(fir: FirBinaryLogicExpression): BinaryAndEnterNode =
BinaryAndEnterNode(graph, fir, levelCounter) BinaryAndEnterNode(graph, fir, levelCounter)
protected fun createWhenBranchConditionEnterNode(fir: FirWhenBranch): WhenBranchConditionEnterNode = fun ControlFlowGraphBuilder.createWhenBranchConditionEnterNode(fir: FirWhenBranch): WhenBranchConditionEnterNode =
WhenBranchConditionEnterNode(graph, fir, levelCounter) WhenBranchConditionEnterNode(graph, fir, levelCounter)
protected fun createWhenEnterNode(fir: FirWhenExpression): WhenEnterNode = fun ControlFlowGraphBuilder.createWhenEnterNode(fir: FirWhenExpression): WhenEnterNode =
WhenEnterNode(graph, fir, levelCounter) WhenEnterNode(graph, fir, levelCounter)
protected fun createWhenExitNode(fir: FirWhenExpression): WhenExitNode = fun ControlFlowGraphBuilder.createWhenExitNode(fir: FirWhenExpression): WhenExitNode =
WhenExitNode(graph, fir, levelCounter) WhenExitNode(graph, fir, levelCounter)
protected fun createWhenBranchResultExitNode(fir: FirWhenBranch): WhenBranchResultExitNode = fun ControlFlowGraphBuilder.createWhenBranchResultExitNode(fir: FirWhenBranch): WhenBranchResultExitNode =
WhenBranchResultExitNode(graph, fir, levelCounter) WhenBranchResultExitNode(graph, fir, levelCounter)
protected fun createLoopConditionExitNode(fir: FirExpression): LoopConditionExitNode = fun ControlFlowGraphBuilder.createLoopConditionExitNode(fir: FirExpression): LoopConditionExitNode =
LoopConditionExitNode(graph, fir, levelCounter) LoopConditionExitNode(graph, fir, levelCounter)
protected fun createLoopConditionEnterNode(fir: FirExpression): LoopConditionEnterNode = fun ControlFlowGraphBuilder.createLoopConditionEnterNode(fir: FirExpression): LoopConditionEnterNode =
LoopConditionEnterNode(graph, fir, levelCounter) LoopConditionEnterNode(graph, fir, levelCounter)
protected fun createLoopBlockEnterNode(fir: FirLoop): LoopBlockEnterNode = fun ControlFlowGraphBuilder.createLoopBlockEnterNode(fir: FirLoop): LoopBlockEnterNode =
LoopBlockEnterNode(graph, fir, levelCounter) LoopBlockEnterNode(graph, fir, levelCounter)
protected fun createLoopBlockExitNode(fir: FirLoop): LoopBlockExitNode = fun ControlFlowGraphBuilder.createLoopBlockExitNode(fir: FirLoop): LoopBlockExitNode =
LoopBlockExitNode(graph, fir, levelCounter) LoopBlockExitNode(graph, fir, levelCounter)
protected fun createFunctionCallNode(fir: FirFunctionCall): FunctionCallNode = fun ControlFlowGraphBuilder.createFunctionCallNode(fir: FirFunctionCall): FunctionCallNode =
FunctionCallNode(graph, fir, levelCounter) FunctionCallNode(graph, fir, levelCounter)
protected fun createVariableAssignmentNode(fir: FirVariableAssignment): VariableAssignmentNode = fun ControlFlowGraphBuilder.createVariableAssignmentNode(fir: FirVariableAssignment): VariableAssignmentNode =
VariableAssignmentNode(graph, fir, levelCounter) VariableAssignmentNode(graph, fir, levelCounter)
protected fun createAnnotationExitNode(fir: FirAnnotationCall): AnnotationExitNode = fun ControlFlowGraphBuilder.createAnnotationExitNode(fir: FirAnnotationCall): AnnotationExitNode =
AnnotationExitNode(graph, fir, levelCounter) AnnotationExitNode(graph, fir, levelCounter)
protected fun createAnnotationEnterNode(fir: FirAnnotationCall): AnnotationEnterNode = fun ControlFlowGraphBuilder.createAnnotationEnterNode(fir: FirAnnotationCall): AnnotationEnterNode =
AnnotationEnterNode(graph, fir, levelCounter) AnnotationEnterNode(graph, fir, levelCounter)
protected fun createVariableDeclarationNode(fir: FirVariable<*>): VariableDeclarationNode = fun ControlFlowGraphBuilder.createVariableDeclarationNode(fir: FirVariable<*>): VariableDeclarationNode =
VariableDeclarationNode(graph, fir, levelCounter) VariableDeclarationNode(graph, fir, levelCounter)
protected fun createConstExpressionNode(fir: FirConstExpression<*>): ConstExpressionNode = fun ControlFlowGraphBuilder.createConstExpressionNode(fir: FirConstExpression<*>): ConstExpressionNode =
ConstExpressionNode(graph, fir, levelCounter) ConstExpressionNode(graph, fir, levelCounter)
protected fun createThrowExceptionNode(fir: FirThrowExpression): ThrowExceptionNode = fun ControlFlowGraphBuilder.createThrowExceptionNode(fir: FirThrowExpression): ThrowExceptionNode =
ThrowExceptionNode(graph, fir, levelCounter) ThrowExceptionNode(graph, fir, levelCounter)
protected fun createFinallyProxyExitNode(fir: FirTryExpression): FinallyProxyExitNode = fun ControlFlowGraphBuilder.createFinallyProxyExitNode(fir: FirTryExpression): FinallyProxyExitNode =
FinallyProxyExitNode(graph, fir, levelCounter) FinallyProxyExitNode(graph, fir, levelCounter)
protected fun createFinallyProxyEnterNode(fir: FirTryExpression): FinallyProxyEnterNode = fun ControlFlowGraphBuilder.createFinallyProxyEnterNode(fir: FirTryExpression): FinallyProxyEnterNode =
FinallyProxyEnterNode(graph, fir, levelCounter) FinallyProxyEnterNode(graph, fir, levelCounter)
protected fun createFinallyBlockExitNode(fir: FirTryExpression): FinallyBlockExitNode = fun ControlFlowGraphBuilder.createFinallyBlockExitNode(fir: FirTryExpression): FinallyBlockExitNode =
FinallyBlockExitNode(graph, fir, levelCounter) FinallyBlockExitNode(graph, fir, levelCounter)
protected fun createFinallyBlockEnterNode(fir: FirTryExpression): FinallyBlockEnterNode = fun ControlFlowGraphBuilder.createFinallyBlockEnterNode(fir: FirTryExpression): FinallyBlockEnterNode =
FinallyBlockEnterNode(graph, fir, levelCounter) FinallyBlockEnterNode(graph, fir, levelCounter)
protected fun createCatchClauseExitNode(fir: FirCatch): CatchClauseExitNode = fun ControlFlowGraphBuilder.createCatchClauseExitNode(fir: FirCatch): CatchClauseExitNode =
CatchClauseExitNode(graph, fir, levelCounter) CatchClauseExitNode(graph, fir, levelCounter)
protected fun createTryMainBlockExitNode(fir: FirTryExpression): TryMainBlockExitNode = fun ControlFlowGraphBuilder.createTryMainBlockExitNode(fir: FirTryExpression): TryMainBlockExitNode =
TryMainBlockExitNode(graph, fir, levelCounter) TryMainBlockExitNode(graph, fir, levelCounter)
protected fun createTryMainBlockEnterNode(fir: FirTryExpression): TryMainBlockEnterNode = fun ControlFlowGraphBuilder.createTryMainBlockEnterNode(fir: FirTryExpression): TryMainBlockEnterNode =
TryMainBlockEnterNode(graph, fir, levelCounter) TryMainBlockEnterNode(graph, fir, levelCounter)
protected fun createCatchClauseEnterNode(fir: FirCatch): CatchClauseEnterNode = fun ControlFlowGraphBuilder.createCatchClauseEnterNode(fir: FirCatch): CatchClauseEnterNode =
CatchClauseEnterNode(graph, fir, levelCounter) CatchClauseEnterNode(graph, fir, levelCounter)
protected fun createTryExpressionEnterNode(fir: FirTryExpression): TryExpressionEnterNode = fun ControlFlowGraphBuilder.createTryExpressionEnterNode(fir: FirTryExpression): TryExpressionEnterNode =
TryExpressionEnterNode(graph, fir, levelCounter) TryExpressionEnterNode(graph, fir, levelCounter)
protected fun createTryExpressionExitNode(fir: FirTryExpression): TryExpressionExitNode = fun ControlFlowGraphBuilder.createTryExpressionExitNode(fir: FirTryExpression): TryExpressionExitNode =
TryExpressionExitNode(graph, fir, levelCounter) TryExpressionExitNode(graph, fir, levelCounter)
protected fun createBinaryAndExitLeftOperandNode(fir: FirBinaryLogicExpression): BinaryAndExitLeftOperandNode = fun ControlFlowGraphBuilder.createBinaryAndExitLeftOperandNode(fir: FirBinaryLogicExpression): BinaryAndExitLeftOperandNode =
BinaryAndExitLeftOperandNode(graph, fir, levelCounter) BinaryAndExitLeftOperandNode(graph, fir, levelCounter)
protected fun createBinaryAndEnterRightOperandNode(fir: FirBinaryLogicExpression): BinaryAndEnterRightOperandNode = fun ControlFlowGraphBuilder.createBinaryAndEnterRightOperandNode(fir: FirBinaryLogicExpression): BinaryAndEnterRightOperandNode =
BinaryAndEnterRightOperandNode(graph, fir, levelCounter) BinaryAndEnterRightOperandNode(graph, fir, levelCounter)
protected fun createBinaryOrEnterRightOperandNode(fir: FirBinaryLogicExpression): BinaryOrEnterRightOperandNode = fun ControlFlowGraphBuilder.createBinaryOrEnterRightOperandNode(fir: FirBinaryLogicExpression): BinaryOrEnterRightOperandNode =
BinaryOrEnterRightOperandNode(graph, fir, levelCounter) BinaryOrEnterRightOperandNode(graph, fir, levelCounter)
}