Less use of KtScope

This commit is contained in:
Valentin Kipyatkov
2015-10-24 12:49:15 +03:00
parent c93de93332
commit 3eb1cff36a
27 changed files with 156 additions and 149 deletions
@@ -17,41 +17,40 @@
package org.jetbrains.kotlin.idea.core
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.idea.util.FuzzyType
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.resolve.BindingTraceContext
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo
import org.jetbrains.kotlin.resolve.scopes.KtScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.collectAllFromMeAndParent
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.expressions.ExpressionTypingContext
import org.jetbrains.kotlin.types.expressions.ForLoopConventionsChecker
import java.util.HashMap
import java.util.*
public class IterableTypesDetection(
private val project: Project,
private val moduleDescriptor: ModuleDescriptor,
private val forLoopConventionsChecker: ForLoopConventionsChecker
) {
companion object {
private val iteratorName = Name.identifier("iterator")
}
public fun createDetector(scope: KtScope): IterableTypesDetector {
public fun createDetector(scope: LexicalScope): IterableTypesDetector {
return Detector(scope)
}
private inner class Detector(private val scope: KtScope): IterableTypesDetector {
private inner class Detector(private val scope: LexicalScope): IterableTypesDetector {
private val cache = HashMap<FuzzyType, FuzzyType?>()
private val typesWithExtensionIterator: Collection<KotlinType> = scope.getFunctions(iteratorName, NoLookupLocation.FROM_IDE)
.map { it.getExtensionReceiverParameter() }
private val typesWithExtensionIterator: Collection<KotlinType> = scope
.collectAllFromMeAndParent { it.getDeclaredFunctions(iteratorName, NoLookupLocation.FROM_IDE) }
.map { it.extensionReceiverParameter }
.filterNotNull()
.map { it.getType() }
.map { it.type }
override fun isIterable(type: FuzzyType, loopVarType: KotlinType?): Boolean {
val elementType = elementType(type) ?: return false
@@ -74,7 +73,7 @@ public class IterableTypesDetection(
val expression = KtPsiFactory(project).createExpression("fake")
val expressionReceiver = ExpressionReceiver(expression, type.type)
val context = ExpressionTypingContext.newContext(BindingTraceContext(), scope.asLexicalScope(), DataFlowInfo.EMPTY, TypeUtils.NO_EXPECTED_TYPE)
val context = ExpressionTypingContext.newContext(BindingTraceContext(), scope, DataFlowInfo.EMPTY, TypeUtils.NO_EXPECTED_TYPE)
val elementType = forLoopConventionsChecker.checkIterableConvention(expressionReceiver, context)
return elementType?.let { FuzzyType(it, type.freeParameters) }
}
@@ -32,11 +32,10 @@ import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.scopes.KtScope
import org.jetbrains.kotlin.resolve.scopes.utils.asKtScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.utils.getClassifier
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import java.util.Collections
import java.util.HashSet
import java.util.*
public class CollectingNameValidator @JvmOverloads constructor(
existingNames: Collection<String> = Collections.emptySet(),
@@ -80,7 +79,7 @@ public class NewDeclarationNameValidator(
if (visibleDeclarationsContext != null) {
val bindingContext = visibleDeclarationsContext.analyze(BodyResolveMode.PARTIAL_FOR_COMPLETION)
val resolutionScope = visibleDeclarationsContext.getResolutionScope(bindingContext, visibleDeclarationsContext.getResolutionFacade())
if (resolutionScope.asKtScope().hasConflict(identifier)) return false
if (resolutionScope.hasConflict(identifier)) return false
}
return checkDeclarationsIn.none {
@@ -88,12 +87,10 @@ public class NewDeclarationNameValidator(
}
}
private fun KtScope.hasConflict(name: Name): Boolean {
val inDeclaration = getContainingDeclaration()
private fun LexicalScope.hasConflict(name: Name): Boolean {
fun DeclarationDescriptor.isVisible(): Boolean {
return when (this) {
is DeclarationDescriptorWithVisibility -> isVisible(inDeclaration)
is DeclarationDescriptorWithVisibility -> isVisible(ownerDescriptor)
else -> true
}
}
@@ -41,10 +41,8 @@ import org.jetbrains.kotlin.resolve.calls.context.CheckArgumentTypesMode
import org.jetbrains.kotlin.resolve.calls.context.ContextDependency
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.results.ResolutionStatus
import org.jetbrains.kotlin.resolve.scopes.KtScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.receivers.ThisReceiver
import org.jetbrains.kotlin.resolve.scopes.utils.asKtScope
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
@@ -93,7 +91,7 @@ public fun Call.mapArgumentsToParameters(targetDescriptor: CallableDescriptor):
return map
}
public fun ThisReceiver.asExpression(resolutionScope: KtScope, psiFactory: KtPsiFactory): KtExpression? {
public fun ThisReceiver.asExpression(resolutionScope: LexicalScope, psiFactory: KtPsiFactory): KtExpression? {
val expressionFactory = resolutionScope.getImplicitReceiversWithInstanceToExpression()
.entrySet()
.firstOrNull { it.key.getContainingDeclaration() == this.getDeclarationDescriptor() }
@@ -178,7 +176,7 @@ private fun expectedType(call: Call, bindingContext: BindingContext): KotlinType
fun KtCallableDeclaration.canOmitDeclaredType(initializerOrBodyExpression: KtExpression, canChangeTypeToSubtype: Boolean): Boolean {
val declaredType = (resolveToDescriptor() as? CallableDescriptor)?.returnType ?: return false
val bindingContext = initializerOrBodyExpression.analyze()
val scope = initializerOrBodyExpression.getResolutionScope(bindingContext, initializerOrBodyExpression.getResolutionFacade()).asKtScope()
val scope = initializerOrBodyExpression.getResolutionScope(bindingContext, initializerOrBodyExpression.getResolutionFacade())
val expressionType = initializerOrBodyExpression.computeTypeInContext(scope) ?: return false
if (KotlinTypeChecker.DEFAULT.equalTypes(expressionType, declaredType)) return true
return canChangeTypeToSubtype && expressionType.isSubtypeOf(declaredType)