From beaab19eb9563227a2b1f4dc1368f89d835e5822 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 4 Sep 2019 19:10:34 +0300 Subject: [PATCH] [FIR] Replace ControlFlowGraphNodeBuilder with extensions on ControlFlowGraphBuilder --- .../dfa/cfg/ControlFlowGraphBuilder.kt | 7 +- .../dfa/cfg/ControlFlowGraphNodeBuilder.kt | 206 +++++++++--------- 2 files changed, 103 insertions(+), 110 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt index 370c0d08be2..aa368ceab02 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphBuilder.kt @@ -15,9 +15,9 @@ import org.jetbrains.kotlin.fir.resolve.dfa.* import org.jetbrains.kotlin.fir.resolve.transformers.resultType import org.jetbrains.kotlin.fir.types.isNothing -class ControlFlowGraphBuilder : ControlFlowGraphNodeBuilder() { +class ControlFlowGraphBuilder { private val graphs: Stack = stackOf(ControlFlowGraph("")) - override val graph: ControlFlowGraph get() = graphs.top() + val graph: ControlFlowGraph get() = graphs.top() private val lexicalScopes: Stack>> = stackOf(stackOf()) private val lastNodes: Stack> get() = lexicalScopes.top() @@ -49,7 +49,8 @@ class ControlFlowGraphBuilder : ControlFlowGraphNodeBuilder() { popCallback = { exitNodes.pop() } ) - override var levelCounter: Int = 0 + var levelCounter: Int = 0 + private set fun isTopLevel(): Boolean = graphs.size == 1 diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphNodeBuilder.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphNodeBuilder.kt index 84cf00ed4f9..7cde69804e4 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphNodeBuilder.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/cfg/ControlFlowGraphNodeBuilder.kt @@ -10,160 +10,152 @@ import org.jetbrains.kotlin.fir.declarations.FirFunction import org.jetbrains.kotlin.fir.declarations.FirProperty import org.jetbrains.kotlin.fir.expressions.* -abstract class ControlFlowGraphNodeBuilder { - protected abstract val graph: ControlFlowGraph - protected abstract var levelCounter: Int +fun ControlFlowGraphBuilder.createStubNode(): StubNode = StubNode(graph, levelCounter) - protected fun createStubNode(): StubNode = StubNode( - graph, - levelCounter - ) +fun ControlFlowGraphBuilder.createLoopExitNode(fir: FirLoop): LoopExitNode = LoopExitNode(graph, fir, 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 = - InitBlockExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createInitBlockEnterNode(fir: FirAnonymousInitializer): InitBlockEnterNode = + InitBlockEnterNode(graph, fir, levelCounter) - protected fun createInitBlockEnterNode(fir: FirAnonymousInitializer): InitBlockEnterNode = - InitBlockEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createTypeOperatorCallNode(fir: FirTypeOperatorCall): TypeOperatorCallNode = + TypeOperatorCallNode(graph, fir, levelCounter) - protected fun createTypeOperatorCallNode(fir: FirTypeOperatorCall): TypeOperatorCallNode = - TypeOperatorCallNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createOperatorCallNode(fir: FirOperatorCall): OperatorCallNode = + OperatorCallNode(graph, fir, levelCounter) - protected fun createOperatorCallNode(fir: FirOperatorCall): OperatorCallNode = - OperatorCallNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createWhenBranchConditionExitNode(fir: FirWhenBranch): WhenBranchConditionExitNode = + WhenBranchConditionExitNode(graph, fir, levelCounter) - protected fun createWhenBranchConditionExitNode(fir: FirWhenBranch): WhenBranchConditionExitNode = - WhenBranchConditionExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createJumpNode(fir: FirJump<*>): JumpNode = + JumpNode(graph, fir, levelCounter) - protected fun createJumpNode(fir: FirJump<*>): JumpNode = - JumpNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createQualifiedAccessNode(fir: FirQualifiedAccessExpression): QualifiedAccessNode = + QualifiedAccessNode(graph, fir, levelCounter) - protected fun createQualifiedAccessNode(fir: FirQualifiedAccessExpression): QualifiedAccessNode = - QualifiedAccessNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createBlockEnterNode(fir: FirBlock): BlockEnterNode = BlockEnterNode(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 = - PropertyInitializerExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createPropertyInitializerEnterNode(fir: FirProperty): PropertyInitializerEnterNode = + PropertyInitializerEnterNode(graph, fir, levelCounter) - protected fun createPropertyInitializerEnterNode(fir: FirProperty): PropertyInitializerEnterNode = - PropertyInitializerEnterNode(graph, fir, levelCounter) - - protected fun createFunctionEnterNode(fir: FirFunction<*>, isInPlace: Boolean): FunctionEnterNode = - FunctionEnterNode(graph, fir, levelCounter).also { - if (!isInPlace) { - graph.enterNode = it - } +fun ControlFlowGraphBuilder.createFunctionEnterNode(fir: FirFunction<*>, isInPlace: Boolean): FunctionEnterNode = + FunctionEnterNode(graph, fir, levelCounter).also { + if (!isInPlace) { + graph.enterNode = it } + } - protected fun createFunctionExitNode(fir: FirFunction<*>, isInPlace: Boolean): FunctionExitNode = - FunctionExitNode(graph, fir, levelCounter).also { - if (!isInPlace) { - graph.exitNode = it - } +fun ControlFlowGraphBuilder.createFunctionExitNode(fir: FirFunction<*>, isInPlace: Boolean): FunctionExitNode = + FunctionExitNode(graph, fir, levelCounter).also { + if (!isInPlace) { + graph.exitNode = it } + } - protected fun createBinaryOrEnterNode(fir: FirBinaryLogicExpression): BinaryOrEnterNode = - BinaryOrEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createBinaryOrEnterNode(fir: FirBinaryLogicExpression): BinaryOrEnterNode = + BinaryOrEnterNode(graph, fir, levelCounter) - protected fun createBinaryOrExitLeftOperandNode(fir: FirBinaryLogicExpression): BinaryOrExitLeftOperandNode = - BinaryOrExitLeftOperandNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createBinaryOrExitLeftOperandNode(fir: FirBinaryLogicExpression): BinaryOrExitLeftOperandNode = + BinaryOrExitLeftOperandNode(graph, fir, levelCounter) - protected fun createBinaryOrExitNode(fir: FirBinaryLogicExpression): BinaryOrExitNode = - BinaryOrExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createBinaryOrExitNode(fir: FirBinaryLogicExpression): BinaryOrExitNode = + BinaryOrExitNode(graph, fir, levelCounter) - protected fun createBinaryAndExitNode(fir: FirBinaryLogicExpression): BinaryAndExitNode = - BinaryAndExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createBinaryAndExitNode(fir: FirBinaryLogicExpression): BinaryAndExitNode = + BinaryAndExitNode(graph, fir, levelCounter) - protected fun createBinaryAndEnterNode(fir: FirBinaryLogicExpression): BinaryAndEnterNode = - BinaryAndEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createBinaryAndEnterNode(fir: FirBinaryLogicExpression): BinaryAndEnterNode = + BinaryAndEnterNode(graph, fir, levelCounter) - protected fun createWhenBranchConditionEnterNode(fir: FirWhenBranch): WhenBranchConditionEnterNode = - WhenBranchConditionEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createWhenBranchConditionEnterNode(fir: FirWhenBranch): WhenBranchConditionEnterNode = + WhenBranchConditionEnterNode(graph, fir, levelCounter) - protected fun createWhenEnterNode(fir: FirWhenExpression): WhenEnterNode = - WhenEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createWhenEnterNode(fir: FirWhenExpression): WhenEnterNode = + WhenEnterNode(graph, fir, levelCounter) - protected fun createWhenExitNode(fir: FirWhenExpression): WhenExitNode = - WhenExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createWhenExitNode(fir: FirWhenExpression): WhenExitNode = + WhenExitNode(graph, fir, levelCounter) - protected fun createWhenBranchResultExitNode(fir: FirWhenBranch): WhenBranchResultExitNode = - WhenBranchResultExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createWhenBranchResultExitNode(fir: FirWhenBranch): WhenBranchResultExitNode = + WhenBranchResultExitNode(graph, fir, levelCounter) - protected fun createLoopConditionExitNode(fir: FirExpression): LoopConditionExitNode = - LoopConditionExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createLoopConditionExitNode(fir: FirExpression): LoopConditionExitNode = + LoopConditionExitNode(graph, fir, levelCounter) - protected fun createLoopConditionEnterNode(fir: FirExpression): LoopConditionEnterNode = - LoopConditionEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createLoopConditionEnterNode(fir: FirExpression): LoopConditionEnterNode = + LoopConditionEnterNode(graph, fir, levelCounter) - protected fun createLoopBlockEnterNode(fir: FirLoop): LoopBlockEnterNode = - LoopBlockEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createLoopBlockEnterNode(fir: FirLoop): LoopBlockEnterNode = + LoopBlockEnterNode(graph, fir, levelCounter) - protected fun createLoopBlockExitNode(fir: FirLoop): LoopBlockExitNode = - LoopBlockExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createLoopBlockExitNode(fir: FirLoop): LoopBlockExitNode = + LoopBlockExitNode(graph, fir, levelCounter) - protected fun createFunctionCallNode(fir: FirFunctionCall): FunctionCallNode = - FunctionCallNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createFunctionCallNode(fir: FirFunctionCall): FunctionCallNode = + FunctionCallNode(graph, fir, levelCounter) - protected fun createVariableAssignmentNode(fir: FirVariableAssignment): VariableAssignmentNode = - VariableAssignmentNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createVariableAssignmentNode(fir: FirVariableAssignment): VariableAssignmentNode = + VariableAssignmentNode(graph, fir, levelCounter) - protected fun createAnnotationExitNode(fir: FirAnnotationCall): AnnotationExitNode = - AnnotationExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createAnnotationExitNode(fir: FirAnnotationCall): AnnotationExitNode = + AnnotationExitNode(graph, fir, levelCounter) - protected fun createAnnotationEnterNode(fir: FirAnnotationCall): AnnotationEnterNode = - AnnotationEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createAnnotationEnterNode(fir: FirAnnotationCall): AnnotationEnterNode = + AnnotationEnterNode(graph, fir, levelCounter) - protected fun createVariableDeclarationNode(fir: FirVariable<*>): VariableDeclarationNode = - VariableDeclarationNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createVariableDeclarationNode(fir: FirVariable<*>): VariableDeclarationNode = + VariableDeclarationNode(graph, fir, levelCounter) - protected fun createConstExpressionNode(fir: FirConstExpression<*>): ConstExpressionNode = - ConstExpressionNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createConstExpressionNode(fir: FirConstExpression<*>): ConstExpressionNode = + ConstExpressionNode(graph, fir, levelCounter) - protected fun createThrowExceptionNode(fir: FirThrowExpression): ThrowExceptionNode = - ThrowExceptionNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createThrowExceptionNode(fir: FirThrowExpression): ThrowExceptionNode = + ThrowExceptionNode(graph, fir, levelCounter) - protected fun createFinallyProxyExitNode(fir: FirTryExpression): FinallyProxyExitNode = - FinallyProxyExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createFinallyProxyExitNode(fir: FirTryExpression): FinallyProxyExitNode = + FinallyProxyExitNode(graph, fir, levelCounter) - protected fun createFinallyProxyEnterNode(fir: FirTryExpression): FinallyProxyEnterNode = - FinallyProxyEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createFinallyProxyEnterNode(fir: FirTryExpression): FinallyProxyEnterNode = + FinallyProxyEnterNode(graph, fir, levelCounter) - protected fun createFinallyBlockExitNode(fir: FirTryExpression): FinallyBlockExitNode = - FinallyBlockExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createFinallyBlockExitNode(fir: FirTryExpression): FinallyBlockExitNode = + FinallyBlockExitNode(graph, fir, levelCounter) - protected fun createFinallyBlockEnterNode(fir: FirTryExpression): FinallyBlockEnterNode = - FinallyBlockEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createFinallyBlockEnterNode(fir: FirTryExpression): FinallyBlockEnterNode = + FinallyBlockEnterNode(graph, fir, levelCounter) - protected fun createCatchClauseExitNode(fir: FirCatch): CatchClauseExitNode = - CatchClauseExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createCatchClauseExitNode(fir: FirCatch): CatchClauseExitNode = + CatchClauseExitNode(graph, fir, levelCounter) - protected fun createTryMainBlockExitNode(fir: FirTryExpression): TryMainBlockExitNode = - TryMainBlockExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createTryMainBlockExitNode(fir: FirTryExpression): TryMainBlockExitNode = + TryMainBlockExitNode(graph, fir, levelCounter) - protected fun createTryMainBlockEnterNode(fir: FirTryExpression): TryMainBlockEnterNode = - TryMainBlockEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createTryMainBlockEnterNode(fir: FirTryExpression): TryMainBlockEnterNode = + TryMainBlockEnterNode(graph, fir, levelCounter) - protected fun createCatchClauseEnterNode(fir: FirCatch): CatchClauseEnterNode = - CatchClauseEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createCatchClauseEnterNode(fir: FirCatch): CatchClauseEnterNode = + CatchClauseEnterNode(graph, fir, levelCounter) - protected fun createTryExpressionEnterNode(fir: FirTryExpression): TryExpressionEnterNode = - TryExpressionEnterNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createTryExpressionEnterNode(fir: FirTryExpression): TryExpressionEnterNode = + TryExpressionEnterNode(graph, fir, levelCounter) - protected fun createTryExpressionExitNode(fir: FirTryExpression): TryExpressionExitNode = - TryExpressionExitNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createTryExpressionExitNode(fir: FirTryExpression): TryExpressionExitNode = + TryExpressionExitNode(graph, fir, levelCounter) - protected fun createBinaryAndExitLeftOperandNode(fir: FirBinaryLogicExpression): BinaryAndExitLeftOperandNode = - BinaryAndExitLeftOperandNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createBinaryAndExitLeftOperandNode(fir: FirBinaryLogicExpression): BinaryAndExitLeftOperandNode = + BinaryAndExitLeftOperandNode(graph, fir, levelCounter) - protected fun createBinaryAndEnterRightOperandNode(fir: FirBinaryLogicExpression): BinaryAndEnterRightOperandNode = - BinaryAndEnterRightOperandNode(graph, fir, levelCounter) +fun ControlFlowGraphBuilder.createBinaryAndEnterRightOperandNode(fir: FirBinaryLogicExpression): BinaryAndEnterRightOperandNode = + BinaryAndEnterRightOperandNode(graph, fir, levelCounter) - protected fun createBinaryOrEnterRightOperandNode(fir: FirBinaryLogicExpression): BinaryOrEnterRightOperandNode = - BinaryOrEnterRightOperandNode(graph, fir, levelCounter) -} \ No newline at end of file +fun ControlFlowGraphBuilder.createBinaryOrEnterRightOperandNode(fir: FirBinaryLogicExpression): BinaryOrEnterRightOperandNode = + BinaryOrEnterRightOperandNode(graph, fir, levelCounter) \ No newline at end of file