Minor code refactorings
This commit is contained in:
@@ -16,21 +16,19 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.resolve.lazy
|
package org.jetbrains.kotlin.resolve.lazy
|
||||||
|
|
||||||
import java.util.HashSet
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.psi.*
|
import com.intellij.util.SmartList
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.siblings
|
import org.jetbrains.kotlin.JetNodeTypes
|
||||||
import org.jetbrains.kotlin.lexer.JetTokens
|
import org.jetbrains.kotlin.lexer.JetTokens
|
||||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||||
|
import org.jetbrains.kotlin.resolve.StatementFilter
|
||||||
|
import org.jetbrains.kotlin.util.isProbablyNothing
|
||||||
|
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||||
|
import org.jetbrains.kotlin.utils.addToStdlib.swap
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
import java.util.HashMap
|
import java.util.HashMap
|
||||||
import com.intellij.psi.PsiElement
|
import java.util.HashSet
|
||||||
import org.jetbrains.kotlin.JetNodeTypes
|
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
|
||||||
import org.jetbrains.kotlin.resolve.StatementFilter
|
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.swap
|
|
||||||
import org.jetbrains.kotlin.util.isProbablyNothing
|
|
||||||
|
|
||||||
//TODO: do resolve anonymous object's body
|
//TODO: do resolve anonymous object's body
|
||||||
|
|
||||||
@@ -56,32 +54,22 @@ class PartialBodyResolveFilter(
|
|||||||
assert(!JetPsiUtil.isLocal(declaration),
|
assert(!JetPsiUtil.isLocal(declaration),
|
||||||
"Should never be invoked on local declaration otherwise we may miss some local declarations with type Nothing")
|
"Should never be invoked on local declaration otherwise we may miss some local declarations with type Nothing")
|
||||||
|
|
||||||
declaration.accept(object : JetVisitorVoid() {
|
declaration.forEachDescendantOfType<JetCallableDeclaration> { declaration ->
|
||||||
override fun visitDeclaration(declaration: JetDeclaration) {
|
if (declaration.getTypeReference().containsProbablyNothing()) {
|
||||||
super.visitDeclaration(declaration)
|
val name = declaration.getName()
|
||||||
|
if (name != null) {
|
||||||
if (declaration is JetCallableDeclaration) {
|
if (declaration is JetNamedFunction) {
|
||||||
if (declaration.getTypeReference().containsProbablyNothing()) {
|
nothingFunctionNames.add(name)
|
||||||
val name = declaration.getName()
|
}
|
||||||
if (name != null) {
|
else {
|
||||||
if (declaration is JetNamedFunction) {
|
nothingVariableNames.add(name)
|
||||||
nothingFunctionNames.add(name)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
nothingVariableNames.add(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
override fun visitElement(element: PsiElement) {
|
|
||||||
element.acceptChildren(this)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
statementMarks.mark(elementToResolve, if (forCompletion) MarkLevel.NEED_COMPLETION else MarkLevel.NEED_REFERENCE_RESOLVE)
|
statementMarks.mark(elementToResolve, if (forCompletion) MarkLevel.NEED_COMPLETION else MarkLevel.NEED_REFERENCE_RESOLVE)
|
||||||
declaration.blocks().forEach { processBlock(it) }
|
declaration.forEachBlock { processBlock(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: do..while is special case
|
//TODO: do..while is special case
|
||||||
@@ -135,7 +123,7 @@ class PartialBodyResolveFilter(
|
|||||||
|
|
||||||
val level = statementMarks.statementMark(statement)
|
val level = statementMarks.statementMark(statement)
|
||||||
if (level > MarkLevel.TAKE) { // otherwise there are no statements inside that need processBlock which only works when reference resolve needed
|
if (level > MarkLevel.TAKE) { // otherwise there are no statements inside that need processBlock which only works when reference resolve needed
|
||||||
for (nestedBlock in statement.blocks()) {
|
statement.forEachBlock { nestedBlock ->
|
||||||
val childFilter = processBlock(nestedBlock)
|
val childFilter = processBlock(nestedBlock)
|
||||||
nameFilter.addNamesFromFilter(childFilter)
|
nameFilter.addNamesFromFilter(childFilter)
|
||||||
}
|
}
|
||||||
@@ -500,22 +488,11 @@ class PartialBodyResolveFilter(
|
|||||||
val isEmpty: Boolean
|
val isEmpty: Boolean
|
||||||
get() = names?.isEmpty() ?: false
|
get() = names?.isEmpty() ?: false
|
||||||
|
|
||||||
private val addUsedNamesVisitor = object : JetVisitorVoid(){
|
|
||||||
override fun visitSimpleNameExpression(expression: JetSimpleNameExpression) {
|
|
||||||
names!!.add(expression.getReferencedName())
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitJetElement(element: JetElement) {
|
|
||||||
element.acceptChildren(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitBlockExpression(expression: JetBlockExpression) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addUsedNames(statement: JetExpression) {
|
fun addUsedNames(statement: JetExpression) {
|
||||||
if (names != null) {
|
if (names != null) {
|
||||||
statement.accept(addUsedNamesVisitor)
|
statement.forEachDescendantOfType<JetSimpleNameExpression>(canGoInside = { it !is JetBlockExpression }) {
|
||||||
|
names!!.add(it.getReferencedName())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -546,18 +523,8 @@ class PartialBodyResolveFilter(
|
|||||||
return element.parentsWithSelf.takeWhile { it != declaration }.firstOrNull { it.isStatement() } as JetExpression?
|
return element.parentsWithSelf.takeWhile { it != declaration }.firstOrNull { it.isStatement() } as JetExpression?
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun JetElement.blocks(): Collection<JetBlockExpression> {
|
private fun JetElement.forEachBlock(action: (JetBlockExpression) -> Unit) {
|
||||||
val result = ArrayList<JetBlockExpression>(1)
|
forEachDescendantOfType({ it !is JetBlockExpression }, action)
|
||||||
this.accept(object : JetVisitorVoid() {
|
|
||||||
override fun visitBlockExpression(expression: JetBlockExpression) {
|
|
||||||
result.add(expression)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitElement(element: PsiElement) {
|
|
||||||
element.acceptChildren(this)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun JetExpression?.isNullLiteral() = this?.getNode()?.getElementType() == JetNodeTypes.NULL
|
private fun JetExpression?.isNullLiteral() = this?.getNode()?.getElementType() == JetNodeTypes.NULL
|
||||||
@@ -594,21 +561,8 @@ class PartialBodyResolveFilter(
|
|||||||
|
|
||||||
private fun PsiElement.isStatement() = this is JetExpression && getParent() is JetBlockExpression
|
private fun PsiElement.isStatement() = this is JetExpression && getParent() is JetBlockExpression
|
||||||
|
|
||||||
private fun JetTypeReference?.containsProbablyNothing(): Boolean {
|
private fun JetTypeReference?.containsProbablyNothing()
|
||||||
var result = false
|
= this?.getTypeElement()?.anyDescendantOfType<JetUserType> { it.isProbablyNothing() } ?: false
|
||||||
this?.getTypeElement()?.accept(object : JetVisitorVoid() {
|
|
||||||
override fun visitJetElement(element: JetElement) {
|
|
||||||
element.acceptChildren(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitUserType(type: JetUserType) {
|
|
||||||
if (type.isProbablyNothing()) {
|
|
||||||
result = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class StatementMarks {
|
private inner class StatementMarks {
|
||||||
|
|||||||
Reference in New Issue
Block a user