From 806bf3b94277f39db36677eb861a0bc5ef1ab431 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 18 Nov 2014 15:08:44 +0300 Subject: [PATCH] Partial body resolve: more efficient handling of while --- .../resolve/lazy/PartialBodyResolveFilter.kt | 16 ++++++++++++++++ .../resolve/partialBodyResolve/While.dump | 3 +++ .../testData/resolve/partialBodyResolve/While.kt | 9 +++++++++ .../resolve/partialBodyResolve/WhileTrue.dump | 3 +++ .../resolve/partialBodyResolve/WhileTrue.kt | 10 ++++++++++ .../resolve/PartialBodyResolveTestGenerated.java | 12 ++++++++++++ 6 files changed, 53 insertions(+) create mode 100644 idea/testData/resolve/partialBodyResolve/While.dump create mode 100644 idea/testData/resolve/partialBodyResolve/While.kt create mode 100644 idea/testData/resolve/partialBodyResolve/WhileTrue.dump create mode 100644 idea/testData/resolve/partialBodyResolve/WhileTrue.kt diff --git a/idea/ide-common/src/org/jetbrains/jet/lang/resolve/lazy/PartialBodyResolveFilter.kt b/idea/ide-common/src/org/jetbrains/jet/lang/resolve/lazy/PartialBodyResolveFilter.kt index 561549959ae..f7aeda64838 100644 --- a/idea/ide-common/src/org/jetbrains/jet/lang/resolve/lazy/PartialBodyResolveFilter.kt +++ b/idea/ide-common/src/org/jetbrains/jet/lang/resolve/lazy/PartialBodyResolveFilter.kt @@ -24,6 +24,7 @@ import org.jetbrains.jet.utils.addIfNotNull import java.util.ArrayList import java.util.HashMap import com.intellij.psi.PsiElement +import org.jetbrains.jet.JetNodeTypes class PartialBodyResolveFilter(elementToResolve: JetElement, private val body: JetExpression) : (JetElement) -> Boolean { @@ -167,6 +168,18 @@ class PartialBodyResolveFilter(elementToResolve: JetElement, private val body: J expression.getLoopRange()?.accept(this) } + override fun visitWhileExpression(expression: JetWhileExpression) { + val condition = expression.getCondition() + // we need to enter the body only for "while(true)" + //TODO: what about e.g. "1 == 1" + if (condition.isTrueConstant()) { + expression.acceptChildren(this) + } + else { + condition?.accept(this) + } + } + //TODO: when }) @@ -260,6 +273,9 @@ class PartialBodyResolveFilter(elementToResolve: JetElement, private val body: J // private fun JetExpression?.isNullLiteral() = this?.getNode()?.getElementType() == JetNodeTypes.NULL + private fun JetExpression?.isTrueConstant() + = this != null && getNode()?.getElementType() == JetNodeTypes.BOOLEAN_CONSTANT && getText() == "true" + //TODO: review logic private fun isValueNeeded(expression: JetExpression): Boolean { val parent = expression.getParent() diff --git a/idea/testData/resolve/partialBodyResolve/While.dump b/idea/testData/resolve/partialBodyResolve/While.dump new file mode 100644 index 00000000000..d19623a1327 --- /dev/null +++ b/idea/testData/resolve/partialBodyResolve/While.dump @@ -0,0 +1,3 @@ +Resolve target: value-parameter val p: kotlin.Any? smart-casted to kotlin.Any +Skipped statements: +print(p1!!) diff --git a/idea/testData/resolve/partialBodyResolve/While.kt b/idea/testData/resolve/partialBodyResolve/While.kt new file mode 100644 index 00000000000..28aaf5be2ca --- /dev/null +++ b/idea/testData/resolve/partialBodyResolve/While.kt @@ -0,0 +1,9 @@ +fun x(s: Any): Boolean{} + +fun foo(p: Any?, p1: Any?) { + while(x(p!!)) { + print(p1!!) + } + + p.hashCode() +} \ No newline at end of file diff --git a/idea/testData/resolve/partialBodyResolve/WhileTrue.dump b/idea/testData/resolve/partialBodyResolve/WhileTrue.dump new file mode 100644 index 00000000000..e4526bb71de --- /dev/null +++ b/idea/testData/resolve/partialBodyResolve/WhileTrue.dump @@ -0,0 +1,3 @@ +Resolve target: value-parameter val p: kotlin.Any? +Skipped statements: +if (x()) break diff --git a/idea/testData/resolve/partialBodyResolve/WhileTrue.kt b/idea/testData/resolve/partialBodyResolve/WhileTrue.kt new file mode 100644 index 00000000000..ec8d081c6dc --- /dev/null +++ b/idea/testData/resolve/partialBodyResolve/WhileTrue.kt @@ -0,0 +1,10 @@ +fun x(): Boolean{} + +fun foo(p: Any?) { + while(true) { + print(p!!) + if (x()) break + } + + p.hashCode() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/resolve/PartialBodyResolveTestGenerated.java b/idea/tests/org/jetbrains/jet/resolve/PartialBodyResolveTestGenerated.java index 67e517b5df3..19a58e5380d 100644 --- a/idea/tests/org/jetbrains/jet/resolve/PartialBodyResolveTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/resolve/PartialBodyResolveTestGenerated.java @@ -167,4 +167,16 @@ public class PartialBodyResolveTestGenerated extends AbstractPartialBodyResolveT String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/Simple.kt"); doTest(fileName); } + + @TestMetadata("While.kt") + public void testWhile() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/While.kt"); + doTest(fileName); + } + + @TestMetadata("WhileTrue.kt") + public void testWhileTrue() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/WhileTrue.kt"); + doTest(fileName); + } }