Get rid of Guava in CFA (except Multimap things)

This commit is contained in:
Mikhail Glukhikh
2017-06-20 19:13:02 +03:00
parent dfe2c16bc7
commit 564cc27a1d
5 changed files with 33 additions and 46 deletions
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.cfg package org.jetbrains.kotlin.cfg
import com.google.common.collect.Lists
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.IElementType
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
@@ -667,7 +666,7 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
builder.jump(afterCatches, expression) builder.jump(afterCatches, expression)
builder.bindLabel(onException) builder.bindLabel(onException)
val catchLabels = Lists.newLinkedList<Label>() val catchLabels = LinkedList<Label>()
val catchClausesSize = catchClauses.size val catchClausesSize = catchClauses.size
for (i in 0..catchClausesSize - 1 - 1) { for (i in 0..catchClausesSize - 1 - 1) {
catchLabels.add(builder.createUnboundLabel("catch " + i)) catchLabels.add(builder.createUnboundLabel("catch " + i))
@@ -903,14 +902,13 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
private fun jumpCrossesTryCatchBoundary(jumpExpression: KtExpressionWithLabel, jumpTarget: PsiElement): Boolean { private fun jumpCrossesTryCatchBoundary(jumpExpression: KtExpressionWithLabel, jumpTarget: PsiElement): Boolean {
var current = jumpExpression.parent var current = jumpExpression.parent
while (current != null) { while (true) {
when (current) { when (current) {
jumpTarget -> return false jumpTarget -> return false
is KtTryExpression -> return true is KtTryExpression -> return true
else -> current = current.parent else -> current = current.parent
} }
} }
return false
} }
private fun jumpDoesNotCrossFunctionBoundary(jumpExpression: KtExpressionWithLabel, jumpTarget: KtLoopExpression): Boolean { private fun jumpDoesNotCrossFunctionBoundary(jumpExpression: KtExpressionWithLabel, jumpTarget: KtLoopExpression): Boolean {
@@ -993,7 +991,7 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
private fun computePseudoValueForParameter(parameter: KtParameter): PseudoValue { private fun computePseudoValueForParameter(parameter: KtParameter): PseudoValue {
val syntheticValue = createSyntheticValue(parameter, MagicKind.FAKE_INITIALIZER) val syntheticValue = createSyntheticValue(parameter, MagicKind.FAKE_INITIALIZER)
val defaultValue = builder.getBoundValue(parameter.defaultValue) ?: return syntheticValue val defaultValue = builder.getBoundValue(parameter.defaultValue) ?: return syntheticValue
return builder.merge(parameter, Lists.newArrayList(defaultValue, syntheticValue)).outputValue return builder.merge(parameter, arrayListOf(defaultValue, syntheticValue)).outputValue
} }
override fun visitBlockExpression(expression: KtBlockExpression) { override fun visitBlockExpression(expression: KtBlockExpression) {
@@ -1096,8 +1094,7 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
} }
private fun generateAndGetReceiverIfAny(expression: KtExpression): KtExpression? { private fun generateAndGetReceiverIfAny(expression: KtExpression): KtExpression? {
val parent = expression.parent val parent = expression.parent as? KtQualifiedExpression ?: return null
if (parent !is KtQualifiedExpression) return null
if (parent.selectorExpression !== expression) return null if (parent.selectorExpression !== expression) return null
@@ -1119,7 +1116,7 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
visitAssignment(property, getDeferredValue(null), property) visitAssignment(property, getDeferredValue(null), property)
generateInstructions(delegate) generateInstructions(delegate)
if (property.isLocal) { if (property.isLocal) {
generateInitializer(property, createSyntheticValue(property, MagicKind.FAKE_INITIALIZER)); generateInitializer(property, createSyntheticValue(property, MagicKind.FAKE_INITIALIZER))
} }
if (builder.getBoundValue(delegate) != null) { if (builder.getBoundValue(delegate) != null) {
createSyntheticValue(property, MagicKind.VALUE_CONSUMER, delegate) createSyntheticValue(property, MagicKind.VALUE_CONSUMER, delegate)
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.cfg package org.jetbrains.kotlin.cfg
import com.google.common.collect.Maps
import org.jetbrains.kotlin.cfg.pseudocode.Pseudocode import org.jetbrains.kotlin.cfg.pseudocode.Pseudocode
import org.jetbrains.kotlin.cfg.pseudocode.PseudocodeUtil import org.jetbrains.kotlin.cfg.pseudocode.PseudocodeUtil
import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction
@@ -34,18 +33,14 @@ import org.jetbrains.kotlin.resolve.BindingContextUtils.variableDescriptorForDec
import java.util.* import java.util.*
class PseudocodeVariablesData(val pseudocode: Pseudocode, private val bindingContext: BindingContext) { class PseudocodeVariablesData(val pseudocode: Pseudocode, private val bindingContext: BindingContext) {
private val pseudocodeVariableDataCollector: PseudocodeVariableDataCollector private val pseudocodeVariableDataCollector = PseudocodeVariableDataCollector(bindingContext, pseudocode)
private val declaredVariablesForDeclaration = Maps.newHashMap<Pseudocode, Set<VariableDescriptor>>() private val declaredVariablesForDeclaration = hashMapOf<Pseudocode, Set<VariableDescriptor>>()
val variableInitializers: Map<Instruction, Edges<InitControlFlowInfo>> by lazy { val variableInitializers: Map<Instruction, Edges<InitControlFlowInfo>> by lazy {
computeVariableInitializers() computeVariableInitializers()
} }
init {
this.pseudocodeVariableDataCollector = PseudocodeVariableDataCollector(bindingContext, pseudocode)
}
val blockScopeVariableInfo: BlockScopeVariableInfo val blockScopeVariableInfo: BlockScopeVariableInfo
get() = pseudocodeVariableDataCollector.blockScopeVariableInfo get() = pseudocodeVariableDataCollector.blockScopeVariableInfo
@@ -16,7 +16,8 @@
package org.jetbrains.kotlin.cfg.pseudocode package org.jetbrains.kotlin.cfg.pseudocode
import com.google.common.collect.* import com.google.common.collect.HashMultimap
import com.google.common.collect.Multimap
import com.intellij.util.containers.BidirectionalMap import com.intellij.util.containers.BidirectionalMap
import org.jetbrains.kotlin.cfg.Label import org.jetbrains.kotlin.cfg.Label
import org.jetbrains.kotlin.cfg.pseudocode.instructions.* import org.jetbrains.kotlin.cfg.pseudocode.instructions.*
@@ -44,9 +45,9 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
private val elementsToValues = BidirectionalMap<KtElement, PseudoValue>() private val elementsToValues = BidirectionalMap<KtElement, PseudoValue>()
private val valueUsages = Maps.newHashMap<PseudoValue, MutableList<Instruction>>() private val valueUsages = hashMapOf<PseudoValue, MutableList<Instruction>>()
private val mergedValues = Maps.newHashMap<PseudoValue, Set<PseudoValue>>() private val mergedValues = hashMapOf<PseudoValue, Set<PseudoValue>>()
private val sideEffectFree = Sets.newHashSet<Instruction>() private val sideEffectFree = hashSetOf<Instruction>()
override var parent: Pseudocode? = null override var parent: Pseudocode? = null
private set private set
@@ -77,7 +78,7 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
private var postPrecessed = false private var postPrecessed = false
private fun getLocalDeclarations(pseudocode: Pseudocode): Set<LocalFunctionDeclarationInstruction> { private fun getLocalDeclarations(pseudocode: Pseudocode): Set<LocalFunctionDeclarationInstruction> {
val localDeclarations = Sets.newLinkedHashSet<LocalFunctionDeclarationInstruction>() val localDeclarations = linkedSetOf<LocalFunctionDeclarationInstruction>()
for (instruction in (pseudocode as PseudocodeImpl).mutableInstructionList) { for (instruction in (pseudocode as PseudocodeImpl).mutableInstructionList) {
if (instruction is LocalFunctionDeclarationInstruction) { if (instruction is LocalFunctionDeclarationInstruction) {
localDeclarations.add(instruction) localDeclarations.add(instruction)
@@ -105,18 +106,17 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
override val reversedInstructions: List<Instruction> override val reversedInstructions: List<Instruction>
get() { get() {
val traversedInstructions = Sets.newLinkedHashSet<Instruction>() val traversedInstructions = linkedSetOf<Instruction>()
traverseFollowingInstructions(sinkInstruction, traversedInstructions, BACKWARD, null) traverseFollowingInstructions(sinkInstruction, traversedInstructions, BACKWARD, null)
if (traversedInstructions.size < instructions.size) { if (traversedInstructions.size < instructions.size) {
val simplyReversedInstructions = Lists.newArrayList(instructions) val simplyReversedInstructions = instructions.reversed()
Collections.reverse(simplyReversedInstructions)
for (instruction in simplyReversedInstructions) { for (instruction in simplyReversedInstructions) {
if (!traversedInstructions.contains(instruction)) { if (!traversedInstructions.contains(instruction)) {
traverseFollowingInstructions(instruction, traversedInstructions, BACKWARD, null) traverseFollowingInstructions(instruction, traversedInstructions, BACKWARD, null)
} }
} }
} }
return Lists.newArrayList(traversedInstructions) return traversedInstructions.toList()
} }
override val instructionsIncludingDeadCode: List<Instruction> override val instructionsIncludingDeadCode: List<Instruction>
@@ -181,7 +181,7 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
override fun getElementValue(element: KtElement?) = elementsToValues[element] override fun getElementValue(element: KtElement?) = elementsToValues[element]
override fun getValueElements(value: PseudoValue?) = elementsToValues.getKeysByValue(value) ?: emptyList() override fun getValueElements(value: PseudoValue?): List<KtElement> = elementsToValues.getKeysByValue(value) ?: emptyList()
override fun getUsages(value: PseudoValue?) = valueUsages[value] ?: mutableListOf() override fun getUsages(value: PseudoValue?) = valueUsages[value] ?: mutableListOf()
@@ -215,7 +215,7 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
if (usage is MergeInstruction) return if (usage is MergeInstruction) return
valueUsages.getOrPut( valueUsages.getOrPut(
value value
) { Lists.newArrayList<Instruction>() }.add(usage) ) { arrayListOf<Instruction>() }.add(usage)
} }
fun postProcess() { fun postProcess() {
@@ -223,11 +223,9 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
postPrecessed = true postPrecessed = true
errorInstruction.sink = sinkInstruction errorInstruction.sink = sinkInstruction
exitInstruction.sink = sinkInstruction exitInstruction.sink = sinkInstruction
var index = 0 for ((index, instruction) in mutableInstructionList.withIndex()) {
for (instruction in mutableInstructionList) {
//recursively invokes 'postProcess' for local declarations //recursively invokes 'postProcess' for local declarations
processInstruction(instruction, index) instruction.processInstruction(index)
index++
} }
if (parent != null) return if (parent != null) return
@@ -249,8 +247,8 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
markDeadInstructions() markDeadInstructions()
} }
private fun processInstruction(instruction: Instruction, currentPosition: Int) { private fun Instruction.processInstruction(currentPosition: Int) {
instruction.accept(object : InstructionVisitor() { accept(object : InstructionVisitor() {
override fun visitInstructionWithNext(instruction: InstructionWithNext) { override fun visitInstructionWithNext(instruction: InstructionWithNext) {
instruction.next = getNextPosition(currentPosition) instruction.next = getNextPosition(currentPosition)
} }
@@ -303,7 +301,7 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
} }
private fun collectReachableInstructions(): Set<Instruction> { private fun collectReachableInstructions(): Set<Instruction> {
val visited = Sets.newHashSet<Instruction>() val visited = hashSetOf<Instruction>()
traverseFollowingInstructions(enterInstruction, visited, FORWARD traverseFollowingInstructions(enterInstruction, visited, FORWARD
) { instruction -> ) { instruction ->
if (instruction is MagicInstruction && instruction.kind === MagicKind.EXHAUSTIVE_WHEN_ELSE) { if (instruction is MagicInstruction && instruction.kind === MagicKind.EXHAUSTIVE_WHEN_ELSE) {
@@ -324,7 +322,7 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
} }
private fun markDeadInstructions() { private fun markDeadInstructions() {
val instructionSet = Sets.newHashSet(instructions) val instructionSet = instructions.toHashSet()
for (instruction in mutableInstructionList) { for (instruction in mutableInstructionList) {
if (!instructionSet.contains(instruction)) { if (!instructionSet.contains(instruction)) {
(instruction as? InstructionImpl)?.markedAsDead = true (instruction as? InstructionImpl)?.markedAsDead = true
@@ -372,7 +370,7 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
val startIndex = startLabel?.targetInstructionIndex ?: 0 val startIndex = startLabel?.targetInstructionIndex ?: 0
val finishIndex = finishLabel?.targetInstructionIndex ?: originalPseudocode.mutableInstructionList.size val finishIndex = finishLabel?.targetInstructionIndex ?: originalPseudocode.mutableInstructionList.size
val originalToCopy = Maps.newLinkedHashMap<Label, PseudocodeLabel>() val originalToCopy = linkedMapOf<Label, PseudocodeLabel>()
val originalLabelsForInstruction = HashMultimap.create<Instruction, Label>() val originalLabelsForInstruction = HashMultimap.create<Instruction, Label>()
for (label in originalPseudocode.labels) { for (label in originalPseudocode.labels) {
val index = label.targetInstructionIndex val index = label.targetInstructionIndex
@@ -381,7 +379,7 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
if (label === startLabel || label === finishLabel) continue if (label === startLabel || label === finishLabel) continue
if (startIndex <= index && index <= finishIndex) { if (index in startIndex..finishIndex) {
originalToCopy.put(label, label.copy(this, labelCount++)) originalToCopy.put(label, label.copy(this, labelCount++))
originalLabelsForInstruction.put(getJumpTarget(label), label) originalLabelsForInstruction.put(getJumpTarget(label), label)
} }
@@ -437,7 +435,7 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode
} }
private fun copyLabels(labels: Collection<Label>, originalToCopy: Map<Label, PseudocodeLabel>): MutableList<Label> { private fun copyLabels(labels: Collection<Label>, originalToCopy: Map<Label, PseudocodeLabel>): MutableList<Label> {
val newLabels = Lists.newArrayList<Label>() val newLabels = arrayListOf<Label>()
for (label in labels) { for (label in labels) {
val newLabel = originalToCopy[label] val newLabel = originalToCopy[label]
newLabels.add(newLabel ?: label) newLabels.add(newLabel ?: label)
@@ -19,8 +19,6 @@ package org.jetbrains.kotlin.cfg.pseudocode.instructions.jumps
import org.jetbrains.kotlin.cfg.pseudocode.PseudoValue import org.jetbrains.kotlin.cfg.pseudocode.PseudoValue
import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.cfg.Label import org.jetbrains.kotlin.cfg.Label
import com.google.common.collect.Maps
import com.google.common.collect.Lists
import org.jetbrains.kotlin.cfg.pseudocode.instructions.BlockScope import org.jetbrains.kotlin.cfg.pseudocode.instructions.BlockScope
import org.jetbrains.kotlin.cfg.pseudocode.instructions.KtElementInstructionImpl import org.jetbrains.kotlin.cfg.pseudocode.instructions.KtElementInstructionImpl
import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction
@@ -35,9 +33,9 @@ class NondeterministicJumpInstruction(
val inputValue: PseudoValue? val inputValue: PseudoValue?
) : KtElementInstructionImpl(element, blockScope), JumpInstruction { ) : KtElementInstructionImpl(element, blockScope), JumpInstruction {
private var _next: Instruction? = null private var _next: Instruction? = null
private val _resolvedTargets: MutableMap<Label, Instruction> = Maps.newLinkedHashMap() private val _resolvedTargets: MutableMap<Label, Instruction> = linkedMapOf()
val targetLabels: List<Label> = Lists.newArrayList(targetLabels) val targetLabels: List<Label> = ArrayList(targetLabels)
val resolvedTargets: Map<Label, Instruction> val resolvedTargets: Map<Label, Instruction>
get() = _resolvedTargets get() = _resolvedTargets
@@ -53,7 +51,7 @@ class NondeterministicJumpInstruction(
override val nextInstructions: Collection<Instruction> override val nextInstructions: Collection<Instruction>
get() { get() {
val targetInstructions = Lists.newArrayList(resolvedTargets.values) val targetInstructions = ArrayList(resolvedTargets.values)
targetInstructions.add(next) targetInstructions.add(next)
return targetInstructions return targetInstructions
} }
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.cfg.pseudocode.instructions.special
import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.cfg.pseudocode.Pseudocode import org.jetbrains.kotlin.cfg.pseudocode.Pseudocode
import com.google.common.collect.Lists
import org.jetbrains.kotlin.cfg.pseudocode.instructions.BlockScope import org.jetbrains.kotlin.cfg.pseudocode.instructions.BlockScope
import org.jetbrains.kotlin.cfg.pseudocode.instructions.InstructionWithNext import org.jetbrains.kotlin.cfg.pseudocode.instructions.InstructionWithNext
import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction
@@ -32,14 +31,14 @@ class LocalFunctionDeclarationInstruction(
blockScope: BlockScope blockScope: BlockScope
) : InstructionWithNext(element, blockScope) { ) : InstructionWithNext(element, blockScope) {
var sink: SubroutineSinkInstruction? = null var sink: SubroutineSinkInstruction? = null
set(value: SubroutineSinkInstruction?) { set(value) {
field = outgoingEdgeTo(value) as SubroutineSinkInstruction? field = outgoingEdgeTo(value) as SubroutineSinkInstruction?
} }
override val nextInstructions: Collection<Instruction> override val nextInstructions: Collection<Instruction>
get() { get() {
if (sink != null) { sink?.let {
val instructions = Lists.newArrayList<Instruction>(sink) val instructions = arrayListOf<Instruction>(it)
instructions.addAll(super.nextInstructions) instructions.addAll(super.nextInstructions)
return instructions return instructions
} }