Analyze Data Flow: Initial implementation

#KT-11994 In Progress
This commit is contained in:
Alexey Sedunov
2017-05-18 18:27:21 +03:00
parent 3f104833ba
commit 858b454138
92 changed files with 1450 additions and 8 deletions
@@ -248,13 +248,13 @@ class ControlFlowInstructionsGenerator : ControlFlowBuilderAdapter() {
override fun returnValue(returnExpression: KtExpression, returnValue: PseudoValue, subroutine: KtElement) {
val exitPoint = getSubroutineExitPoint(subroutine) ?: return
handleJumpInsideTryFinally(exitPoint)
add(ReturnValueInstruction(returnExpression, currentScope, exitPoint, returnValue))
add(ReturnValueInstruction(returnExpression, currentScope, exitPoint, returnValue, subroutine))
}
override fun returnNoValue(returnExpression: KtReturnExpression, subroutine: KtElement) {
val exitPoint = getSubroutineExitPoint(subroutine) ?: return
handleJumpInsideTryFinally(exitPoint)
add(ReturnNoValueInstruction(returnExpression, currentScope, exitPoint))
add(ReturnNoValueInstruction(returnExpression, currentScope, exitPoint, subroutine))
}
override fun write(
@@ -19,6 +19,8 @@ package org.jetbrains.kotlin.cfg.pseudocode.instructions.eval
import org.jetbrains.kotlin.cfg.pseudocode.PseudoValue
import org.jetbrains.kotlin.cfg.pseudocode.PseudoValueFactory
import org.jetbrains.kotlin.cfg.pseudocode.instructions.*
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtNamedDeclaration
@@ -39,6 +41,13 @@ sealed class AccessTarget {
object BlackBox: AccessTarget()
}
val AccessTarget.accessedDescriptor: CallableDescriptor?
get() = when (this) {
is AccessTarget.Declaration -> descriptor
is AccessTarget.Call -> resolvedCall.resultingDescriptor
is AccessTarget.BlackBox -> null
}
abstract class AccessValueInstruction protected constructor(
element: KtElement,
blockScope: BlockScope,
@@ -25,7 +25,8 @@ import org.jetbrains.kotlin.cfg.pseudocode.instructions.InstructionVisitorWithRe
class ReturnNoValueInstruction(
element: KtElement,
blockScope: BlockScope,
targetLabel: Label
targetLabel: Label,
val subroutine: KtElement
) : AbstractJumpInstruction(element, targetLabel, blockScope) {
override fun accept(visitor: InstructionVisitor) {
visitor.visitReturnNoValue(this)
@@ -38,5 +39,5 @@ class ReturnNoValueInstruction(
override fun toString(): String = "ret $targetLabel"
override fun createCopy(newLabel: Label, blockScope: BlockScope): AbstractJumpInstruction =
ReturnNoValueInstruction(element, blockScope, newLabel)
ReturnNoValueInstruction(element, blockScope, newLabel, subroutine)
}
@@ -23,13 +23,15 @@ import java.util.Collections
import org.jetbrains.kotlin.cfg.pseudocode.instructions.BlockScope
import org.jetbrains.kotlin.cfg.pseudocode.instructions.InstructionVisitor
import org.jetbrains.kotlin.cfg.pseudocode.instructions.InstructionVisitorWithResult
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtReturnExpression
class ReturnValueInstruction(
returnExpression: KtExpression,
blockScope: BlockScope,
targetLabel: Label,
val returnedValue: PseudoValue
val returnedValue: PseudoValue,
val subroutine: KtElement
) : AbstractJumpInstruction(returnExpression, targetLabel, blockScope) {
override val inputValues: List<PseudoValue> get() = Collections.singletonList(returnedValue)
@@ -46,7 +48,7 @@ class ReturnValueInstruction(
}
override fun createCopy(newLabel: Label, blockScope: BlockScope): AbstractJumpInstruction {
return ReturnValueInstruction((element as KtExpression), blockScope, newLabel, returnedValue)
return ReturnValueInstruction((element as KtExpression), blockScope, newLabel, returnedValue, subroutine)
}
val returnExpressionIfAny: KtReturnExpression? = element as? KtReturnExpression