diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/AssignedVariablesSearcher.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/AssignedVariablesSearcher.kt new file mode 100644 index 00000000000..afa606cb2be --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/AssignedVariablesSearcher.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.types.expressions + +import org.jetbrains.kotlin.lexer.JetTokens +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.psi.JetBinaryExpression +import org.jetbrains.kotlin.psi.JetNameReferenceExpression +import org.jetbrains.kotlin.psi.JetPsiUtil +import org.jetbrains.kotlin.psi.JetTreeVisitorVoid +import java.util.* + +abstract class AssignedVariablesSearcher: JetTreeVisitorVoid() { + + protected val assignedNames: MutableSet = LinkedHashSet() + + override fun visitBinaryExpression(binaryExpression: JetBinaryExpression) { + if (binaryExpression.operationToken === JetTokens.EQ) { + val left = JetPsiUtil.deparenthesize(binaryExpression.left) + if (left is JetNameReferenceExpression) { + assignedNames += left.getReferencedNameAsName() + } + } + } + +} \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/PreliminaryLoopVisitor.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/PreliminaryLoopVisitor.java index 18b76ffae4c..b5e99e035f6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/PreliminaryLoopVisitor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/PreliminaryLoopVisitor.java @@ -18,8 +18,6 @@ package org.jetbrains.kotlin.types.expressions; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor; -import org.jetbrains.kotlin.lexer.JetTokens; -import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue; @@ -31,20 +29,11 @@ import java.util.*; * The purpose of this class is to find all variable assignments * before loop analysis */ -class PreliminaryLoopVisitor extends JetTreeVisitor { - - // loop under analysis - private final JetLoopExpression loopExpression; - - private final Set assignedNames = new LinkedHashSet(); - - private PreliminaryLoopVisitor(@NotNull JetLoopExpression loopExpression) { - this.loopExpression = loopExpression; - } +class PreliminaryLoopVisitor extends AssignedVariablesSearcher { @NotNull static public PreliminaryLoopVisitor visitLoop(@NotNull JetLoopExpression loopExpression) { - PreliminaryLoopVisitor visitor = new PreliminaryLoopVisitor(loopExpression); + PreliminaryLoopVisitor visitor = new PreliminaryLoopVisitor(); loopExpression.accept(visitor, null); return visitor; } @@ -57,7 +46,7 @@ class PreliminaryLoopVisitor extends JetTreeVisitor { // Only uncaptured local variables are under interest here if (value.isUncapturedLocalVariable() && value.getId() instanceof LocalVariableDescriptor) { LocalVariableDescriptor descriptor = (LocalVariableDescriptor)value.getId(); - if (assignedNames.contains(descriptor.getName())) { + if (getAssignedNames().contains(descriptor.getName())) { valueSetToClear.add(value); } } @@ -67,17 +56,4 @@ class PreliminaryLoopVisitor extends JetTreeVisitor { } return dataFlowInfo; } - - @Override - public Void visitBinaryExpression(@NotNull JetBinaryExpression binaryExpression, Void arg) { - if (binaryExpression.getOperationToken() == JetTokens.EQ) { - JetExpression left = JetPsiUtil.deparenthesize(binaryExpression.getLeft()); - if (left instanceof JetNameReferenceExpression) { - // deparenthesize - assignedNames.add(((JetNameReferenceExpression) left).getReferencedNameAsName()); - } - } - return null; - } - }