Pseudocode: converted to Kotlin

This commit is contained in:
Mikhail Glukhikh
2016-01-26 12:50:53 +03:00
parent c54f8ebac7
commit 1d6eadbd37
4 changed files with 31 additions and 48 deletions
@@ -208,7 +208,7 @@ fun Pseudocode.getStartInstruction(traversalOrder: TraversalOrder): Instruction
fun Pseudocode.getLastInstruction(traversalOrder: TraversalOrder): Instruction = fun Pseudocode.getLastInstruction(traversalOrder: TraversalOrder): Instruction =
if (traversalOrder == FORWARD) sinkInstruction else enterInstruction if (traversalOrder == FORWARD) sinkInstruction else enterInstruction
fun Pseudocode.getInstructions(traversalOrder: TraversalOrder): MutableList<Instruction> = fun Pseudocode.getInstructions(traversalOrder: TraversalOrder): List<Instruction> =
if (traversalOrder == FORWARD) instructions else reversedInstructions if (traversalOrder == FORWARD) instructions else reversedInstructions
fun Instruction.getNextInstructions(traversalOrder: TraversalOrder): Collection<Instruction> = fun Instruction.getNextInstructions(traversalOrder: TraversalOrder): Collection<Instruction> =
@@ -14,58 +14,41 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.cfg.pseudocode; package org.jetbrains.kotlin.cfg.pseudocode
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction
import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction
import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction; import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineEnterInstruction
import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction; import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction
import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineEnterInstruction; import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineSinkInstruction
import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction; import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineSinkInstruction;
import org.jetbrains.kotlin.psi.KtElement;
import java.util.List; interface Pseudocode {
import java.util.Set; val correspondingElement: KtElement
public interface Pseudocode { val parent: Pseudocode?
@NotNull
KtElement getCorrespondingElement();
@Nullable val localDeclarations: Set<LocalFunctionDeclarationInstruction>
Pseudocode getParent();
@NotNull val instructions: List<Instruction>
Set<LocalFunctionDeclarationInstruction> getLocalDeclarations();
@NotNull val reversedInstructions: List<Instruction>
List<Instruction> getInstructions();
@NotNull val instructionsIncludingDeadCode: List<Instruction>
List<Instruction> getReversedInstructions();
@NotNull val exitInstruction: SubroutineExitInstruction
List<Instruction> getInstructionsIncludingDeadCode();
@NotNull val sinkInstruction: SubroutineSinkInstruction
SubroutineExitInstruction getExitInstruction();
@NotNull val enterInstruction: SubroutineEnterInstruction
SubroutineSinkInstruction getSinkInstruction();
@NotNull fun getElementValue(element: KtElement?): PseudoValue?
SubroutineEnterInstruction getEnterInstruction();
@Nullable fun getValueElements(value: PseudoValue?): List<KtElement>
PseudoValue getElementValue(@Nullable KtElement element);
@NotNull fun getUsages(value: PseudoValue?): List<Instruction>
List<? extends KtElement> getValueElements(@Nullable PseudoValue value);
@NotNull fun isSideEffectFree(instruction: Instruction): Boolean
List<? extends Instruction> getUsages(@Nullable PseudoValue value);
boolean isSideEffectFree(@NotNull Instruction instruction); fun copy(): Pseudocode
Pseudocode copy();
} }
@@ -278,15 +278,15 @@ public class PseudocodeImpl implements Pseudocode {
@NotNull @NotNull
@Override @Override
public List<? extends KtElement> getValueElements(@Nullable PseudoValue value) { public List<KtElement> getValueElements(@Nullable PseudoValue value) {
List<? extends KtElement> result = elementsToValues.getKeysByValue(value); List<KtElement> result = elementsToValues.getKeysByValue(value);
return result != null ? result : Collections.<KtElement>emptyList(); return result != null ? result : Collections.<KtElement>emptyList();
} }
@NotNull @NotNull
@Override @Override
public List<? extends Instruction> getUsages(@Nullable PseudoValue value) { public List<Instruction> getUsages(@Nullable PseudoValue value) {
List<? extends Instruction> result = valueUsages.get(value); List<Instruction> result = valueUsages.get(value);
return result != null ? result : Collections.<Instruction>emptyList(); return result != null ? result : Collections.<Instruction>emptyList();
} }
@@ -149,7 +149,7 @@ fun getExpectedTypePredicate(
val returnElement = it.element val returnElement = it.element
val functionDescriptor = when(returnElement) { val functionDescriptor = when(returnElement) {
is KtReturnExpression -> returnElement.getTargetFunctionDescriptor(bindingContext) is KtReturnExpression -> returnElement.getTargetFunctionDescriptor(bindingContext)
else -> bindingContext[DECLARATION_TO_DESCRIPTOR, pseudocode.getCorrespondingElement()] else -> bindingContext[DECLARATION_TO_DESCRIPTOR, pseudocode.correspondingElement]
} }
addSubtypesOf((functionDescriptor as? CallableDescriptor)?.getReturnType()) addSubtypesOf((functionDescriptor as? CallableDescriptor)?.getReturnType())
} }
@@ -278,7 +278,7 @@ fun Pseudocode.getElementValuesRecursively(element: KtElement): List<PseudoValue
fun Pseudocode.collectValues() { fun Pseudocode.collectValues() {
getElementValue(element)?.let { results.add(it) } getElementValue(element)?.let { results.add(it) }
for (localFunction in getLocalDeclarations()) { for (localFunction in localDeclarations) {
localFunction.body.collectValues() localFunction.body.collectValues()
} }
} }
@@ -303,8 +303,8 @@ fun KtElement.getContainingPseudocode(context: BindingContext): Pseudocode? {
} }
fun Pseudocode.getPseudocodeByElement(element: KtElement): Pseudocode? { fun Pseudocode.getPseudocodeByElement(element: KtElement): Pseudocode? {
if (getCorrespondingElement() == element) return this if (correspondingElement == element) return this
getLocalDeclarations().forEach { decl -> decl.body.getPseudocodeByElement(element)?.let { return it } } localDeclarations.forEach { decl -> decl.body.getPseudocodeByElement(element)?.let { return it } }
return null return null
} }