From e91805a59317c0bd64e6686fb2a9e09ff453ec63 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Thu, 13 Dec 2012 17:58:00 +0400 Subject: [PATCH] TraversalOrder enum instead of boolean flag --- .../lang/cfg/JetFlowInformationProvider.java | 13 ++- .../jet/lang/cfg/PseudocodeTraverser.java | 94 ++++++++++--------- .../jet/lang/cfg/PseudocodeVariablesData.java | 9 +- 3 files changed, 62 insertions(+), 54 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java index 594e166fb65..74de8a05827 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java @@ -44,6 +44,8 @@ import org.jetbrains.jet.plugin.JetMainDetector; import java.util.*; +import static org.jetbrains.jet.lang.cfg.PseudocodeTraverser.TraversalOrder.BACKWARD; +import static org.jetbrains.jet.lang.cfg.PseudocodeTraverser.TraversalOrder.FORWARD; import static org.jetbrains.jet.lang.cfg.PseudocodeVariablesData.VariableUseState.*; import static org.jetbrains.jet.lang.diagnostics.Errors.*; import static org.jetbrains.jet.lang.resolve.BindingContext.CAPTURED_IN_CLOSURE; @@ -196,7 +198,7 @@ public class JetFlowInformationProvider { final Map reportedDiagnosticMap = Maps.newHashMap(); - PseudocodeTraverser.traverseForward(pseudocode, true, initializers, new InstructionDataAnalyzeStrategy>() { + PseudocodeTraverser.traverse(pseudocode, FORWARD, true, initializers, new InstructionDataAnalyzeStrategy>() { @Override public void execute(@NotNull Instruction instruction, @Nullable Map in, @@ -251,7 +253,8 @@ public class JetFlowInformationProvider { if (!isInitialized && !varWithUninitializedErrorGenerated.contains(ctxt.variableDescriptor)) { varWithUninitializedErrorGenerated.add(ctxt.variableDescriptor); if (ctxt.variableDescriptor instanceof ValueParameterDescriptor) { - report(Errors.UNINITIALIZED_PARAMETER.on((JetSimpleNameExpression) element, (ValueParameterDescriptor) ctxt.variableDescriptor), ctxt); + report(Errors.UNINITIALIZED_PARAMETER.on((JetSimpleNameExpression) element, + (ValueParameterDescriptor) ctxt.variableDescriptor), ctxt); } else { report(Errors.UNINITIALIZED_VARIABLE.on((JetSimpleNameExpression) element, ctxt.variableDescriptor), ctxt); @@ -502,7 +505,7 @@ public class JetFlowInformationProvider { } } }; - PseudocodeTraverser.traverseBackward(pseudocode, true, variableStatusData, variableStatusAnalyzeStrategy); + PseudocodeTraverser.traverse(pseudocode, BACKWARD, true, variableStatusData, variableStatusAnalyzeStrategy); } //////////////////////////////////////////////////////////////////////////////// @@ -511,8 +514,8 @@ public class JetFlowInformationProvider { public void markUnusedLiteralsInBlock() { assert pseudocode != null; final Map reportedDiagnosticMap = Maps.newHashMap(); - PseudocodeTraverser.traverseForward( - pseudocode, new InstructionAnalyzeStrategy() { + PseudocodeTraverser.traverse( + pseudocode, FORWARD, new InstructionAnalyzeStrategy() { @Override public void execute(@NotNull Instruction instruction) { if (!(instruction instanceof ReadValueInstruction)) return; diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/PseudocodeTraverser.java b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/PseudocodeTraverser.java index 244c37a9abb..ccef1e730a0 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/PseudocodeTraverser.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/PseudocodeTraverser.java @@ -28,29 +28,57 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import static org.jetbrains.jet.lang.cfg.PseudocodeTraverser.TraversalOrder.FORWARD; + /** * @author svtk */ public class PseudocodeTraverser { + + public static enum TraversalOrder { + FORWARD, + BACKWARD; + } + @NotNull - private static Instruction getStartInstruction(@NotNull Pseudocode pseudocode, boolean directOrder) { - return directOrder ? pseudocode.getEnterInstruction() : pseudocode.getSinkInstruction(); + private static Instruction getStartInstruction(@NotNull Pseudocode pseudocode, @NotNull TraversalOrder traversalOrder) { + return traversalOrder == FORWARD ? pseudocode.getEnterInstruction() : pseudocode.getSinkInstruction(); + } + + @NotNull + private static Instruction getLastInstruction(@NotNull Pseudocode pseudocode, @NotNull TraversalOrder traversalOrder) { + return traversalOrder == FORWARD ? pseudocode.getSinkInstruction() : pseudocode.getEnterInstruction(); + } + + @NotNull + private static List getInstructions(@NotNull Pseudocode pseudocode, @NotNull TraversalOrder traversalOrder) { + return traversalOrder == FORWARD ? pseudocode.getInstructions() : pseudocode.getReversedInstructions(); + } + + @NotNull + private static Collection getPreviousInstruction(@NotNull Instruction instruction, @NotNull TraversalOrder traversalOrder) { + return traversalOrder == FORWARD ? instruction.getPreviousInstructions() : instruction.getNextInstructions(); + } + + private static boolean isStartInstruction(@NotNull Instruction instruction, @NotNull TraversalOrder traversalOrder) { + return traversalOrder == FORWARD ? instruction instanceof SubroutineEnterInstruction + : instruction instanceof SubroutineSinkInstruction; } public static Map> collectData( - @NotNull Pseudocode pseudocode, boolean directOrder, boolean lookInside, + @NotNull Pseudocode pseudocode, TraversalOrder traversalOrder, boolean lookInside, @NotNull D initialDataValue, @NotNull D initialDataValueForEnterInstruction, @NotNull InstructionDataMergeStrategy instructionDataMergeStrategy) { Map> edgesMap = Maps.newLinkedHashMap(); initializeEdgesMap(pseudocode, lookInside, edgesMap, initialDataValue); - edgesMap.put(getStartInstruction(pseudocode, directOrder), Edges.create(initialDataValueForEnterInstruction, initialDataValueForEnterInstruction)); + edgesMap.put(getStartInstruction(pseudocode, traversalOrder), Edges.create(initialDataValueForEnterInstruction, initialDataValueForEnterInstruction)); boolean[] changed = new boolean[1]; changed[0] = true; while (changed[0]) { changed[0] = false; - collectDataFromSubgraph(pseudocode, directOrder, lookInside, edgesMap, instructionDataMergeStrategy, + collectDataFromSubgraph(pseudocode, traversalOrder, lookInside, edgesMap, instructionDataMergeStrategy, Collections.emptyList(), changed, false); } return edgesMap; @@ -71,21 +99,21 @@ public class PseudocodeTraverser { } private static void collectDataFromSubgraph( - @NotNull Pseudocode pseudocode, boolean directOrder, boolean lookInside, + @NotNull Pseudocode pseudocode, TraversalOrder traversalOrder, boolean lookInside, @NotNull Map> edgesMap, @NotNull InstructionDataMergeStrategy instructionDataMergeStrategy, @NotNull Collection previousSubGraphInstructions, boolean[] changed, boolean isLocal) { - List instructions = directOrder ? pseudocode.getInstructions() : pseudocode.getReversedInstructions(); - Instruction startInstruction = getStartInstruction(pseudocode, directOrder); + List instructions = getInstructions(pseudocode, traversalOrder); + Instruction startInstruction = getStartInstruction(pseudocode, traversalOrder); for (Instruction instruction : instructions) { - boolean isStart = directOrder ? instruction instanceof SubroutineEnterInstruction : instruction instanceof SubroutineSinkInstruction; + boolean isStart = isStartInstruction(instruction, traversalOrder); if (!isLocal && isStart) continue; Collection allPreviousInstructions; - Collection previousInstructions = directOrder ? instruction.getPreviousInstructions() : instruction.getNextInstructions(); + Collection previousInstructions = getPreviousInstruction(instruction, traversalOrder); if (instruction == startInstruction && !previousSubGraphInstructions.isEmpty()) { allPreviousInstructions = Lists.newArrayList(previousInstructions); @@ -97,10 +125,10 @@ public class PseudocodeTraverser { if (lookInside && instruction instanceof LocalDeclarationInstruction) { Pseudocode subroutinePseudocode = ((LocalDeclarationInstruction) instruction).getBody(); - collectDataFromSubgraph(subroutinePseudocode, directOrder, lookInside, edgesMap, instructionDataMergeStrategy, + collectDataFromSubgraph(subroutinePseudocode, traversalOrder, lookInside, edgesMap, instructionDataMergeStrategy, previousInstructions, changed, true); - Instruction lastInstruction = directOrder ? subroutinePseudocode.getSinkInstruction() : subroutinePseudocode.getEnterInstruction(); + Instruction lastInstruction = getLastInstruction(subroutinePseudocode, traversalOrder); Edges previousValue = edgesMap.get(instruction); Edges newValue = edgesMap.get(lastInstruction); if (!previousValue.equals(newValue)) { @@ -127,54 +155,28 @@ public class PseudocodeTraverser { } } - public static void traverseForward( - @NotNull Pseudocode pseudocode, - @NotNull InstructionAnalyzeStrategy instructionAnalyzeStrategy) { - traverse(pseudocode, true, instructionAnalyzeStrategy); - } - - public static void traverseBackward( - @NotNull Pseudocode pseudocode, - @NotNull InstructionAnalyzeStrategy instructionAnalyzeStrategy) { - traverse(pseudocode, false, instructionAnalyzeStrategy); - } - - private static void traverse( - @NotNull Pseudocode pseudocode, boolean directOrder, + public static void traverse( + @NotNull Pseudocode pseudocode, TraversalOrder traversalOrder, InstructionAnalyzeStrategy instructionAnalyzeStrategy) { - List instructions = directOrder ? pseudocode.getInstructions() : pseudocode.getReversedInstructions(); + List instructions = getInstructions(pseudocode, traversalOrder); for (Instruction instruction : instructions) { if (instruction instanceof LocalDeclarationInstruction) { - traverse(((LocalDeclarationInstruction) instruction).getBody(), directOrder, instructionAnalyzeStrategy); + traverse(((LocalDeclarationInstruction) instruction).getBody(), traversalOrder, instructionAnalyzeStrategy); } instructionAnalyzeStrategy.execute(instruction); } } - public static void traverseForward( - @NotNull Pseudocode pseudocode, boolean lookInside, - @NotNull Map> edgesMap, - @NotNull InstructionDataAnalyzeStrategy instructionDataAnalyzeStrategy) { - traverse(pseudocode, true, lookInside, edgesMap, instructionDataAnalyzeStrategy); - } - - public static void traverseBackward( - @NotNull Pseudocode pseudocode, boolean lookInside, - @NotNull Map> edgesMap, - @NotNull InstructionDataAnalyzeStrategy instructionDataAnalyzeStrategy) { - traverse(pseudocode, false, lookInside, edgesMap, instructionDataAnalyzeStrategy); - } - - private static void traverse( - @NotNull Pseudocode pseudocode, boolean directOrder, boolean lookInside, + public static void traverse( + @NotNull Pseudocode pseudocode, TraversalOrder traversalOrder, boolean lookInside, @NotNull Map> edgesMap, @NotNull InstructionDataAnalyzeStrategy instructionDataAnalyzeStrategy) { - List instructions = directOrder ? pseudocode.getInstructions() : pseudocode.getReversedInstructions(); + List instructions = getInstructions(pseudocode, traversalOrder); for (Instruction instruction : instructions) { if (lookInside && instruction instanceof LocalDeclarationInstruction) { - traverse(((LocalDeclarationInstruction) instruction).getBody(), directOrder, lookInside, edgesMap, + traverse(((LocalDeclarationInstruction) instruction).getBody(), traversalOrder, lookInside, edgesMap, instructionDataAnalyzeStrategy); } Edges edges = edgesMap.get(instruction); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/PseudocodeVariablesData.java b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/PseudocodeVariablesData.java index fea60d5e361..d5da09374da 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/PseudocodeVariablesData.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/PseudocodeVariablesData.java @@ -33,6 +33,9 @@ import java.util.Collections; import java.util.Map; import java.util.Set; +import static org.jetbrains.jet.lang.cfg.PseudocodeTraverser.TraversalOrder.BACKWARD; +import static org.jetbrains.jet.lang.cfg.PseudocodeTraverser.TraversalOrder.FORWARD; + /** * @author svtk */ @@ -61,7 +64,7 @@ public class PseudocodeVariablesData { Set usedVariables = usedVariablesForDeclaration.get(pseudocode); if (usedVariables == null) { final Set result = Sets.newHashSet(); - PseudocodeTraverser.traverseForward(pseudocode, new InstructionAnalyzeStrategy() { + PseudocodeTraverser.traverse(pseudocode, FORWARD, new InstructionAnalyzeStrategy() { @Override public void execute(@NotNull Instruction instruction) { VariableDescriptor variableDescriptor = PseudocodeUtil.extractVariableDescriptorIfAny(instruction, false, @@ -138,7 +141,7 @@ public class PseudocodeVariablesData { usedVariables, declaredVariables); Map>> variableInitializersMap = PseudocodeTraverser.collectData( - pseudocode, /* directOrder = */ true, /* lookInside = */ false, + pseudocode, FORWARD, /* lookInside = */ false, initialMap, initialMapForStartInstruction, new PseudocodeTraverser.InstructionDataMergeStrategy>() { @Override public Edges> execute( @@ -289,7 +292,7 @@ public class PseudocodeVariablesData { return Edges.create(enterResult, exitResult); } }; - variableStatusMap = PseudocodeTraverser.collectData(pseudocode, false, true, + variableStatusMap = PseudocodeTraverser.collectData(pseudocode, BACKWARD, true, Collections.emptyMap(), sinkInstructionData, collectVariableUseStatusStrategy); }