From 22e667ad65c9f65100d80b1bd1d9a05852bff23d Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Fri, 9 Sep 2016 20:23:30 +0300 Subject: [PATCH] Removed generic hell from tower resolver --- .../calls/tower/NewResolutionOldInference.kt | 68 +++++++++---------- .../resolve/calls/tower/ImplicitScopeTower.kt | 12 ++-- .../resolve/calls/tower/InvokeProcessors.kt | 50 +++++++------- .../calls/tower/ScopeTowerProcessors.kt | 58 ++++++++-------- .../kotlin/resolve/calls/tower/TowerLevels.kt | 43 ++++++------ .../resolve/calls/tower/TowerResolver.kt | 15 ++-- .../kotlin/resolve/calls/tower/TowerUtils.kt | 10 +-- 7 files changed, 125 insertions(+), 131 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt index 892bdf5e2ec..09c8afb6101 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt @@ -78,14 +78,14 @@ class NewResolutionOldInference( scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext - ): ScopeTowerProcessor> + ): ScopeTowerProcessor object Function : ResolutionKind() { override fun createTowerProcessor( outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext - ): ScopeTowerProcessor> { - val functionFactory = outer.CandidateFactoryImpl(name, context, tracing) + ): ScopeTowerProcessor { + val functionFactory = outer.CandidateFactoryImpl(name, context, tracing) return createFunctionProcessor(scopeTower, name, functionFactory, outer.CandidateFactoryProviderForInvokeImpl(functionFactory), explicitReceiver) } } @@ -94,8 +94,8 @@ class NewResolutionOldInference( override fun createTowerProcessor( outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext - ): ScopeTowerProcessor> { - val variableFactory = outer.CandidateFactoryImpl(name, context, tracing) + ): ScopeTowerProcessor { + val variableFactory = outer.CandidateFactoryImpl(name, context, tracing) return createVariableAndObjectProcessor(scopeTower, name, variableFactory, explicitReceiver) } } @@ -104,9 +104,9 @@ class NewResolutionOldInference( override fun createTowerProcessor( outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext - ): ScopeTowerProcessor> { - val functionFactory = outer.CandidateFactoryImpl(name, context, tracing) - val variableFactory = outer.CandidateFactoryImpl(name, context, tracing) + ): ScopeTowerProcessor { + val functionFactory = outer.CandidateFactoryImpl(name, context, tracing) + val variableFactory = outer.CandidateFactoryImpl(name, context, tracing) return CompositeScopeTowerProcessor( createSimpleFunctionProcessor(scopeTower, name, functionFactory, explicitReceiver, classValueReceiver = false), createVariableProcessor(scopeTower, name, variableFactory, explicitReceiver, classValueReceiver = false) @@ -118,8 +118,8 @@ class NewResolutionOldInference( override fun createTowerProcessor( outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext - ): ScopeTowerProcessor> { - val functionFactory = outer.CandidateFactoryImpl(name, context, tracing) + ): ScopeTowerProcessor { + val functionFactory = outer.CandidateFactoryImpl(name, context, tracing) // todo val call = (context.call as? CallTransformer.CallForImplicitInvoke).sure { "Call should be CallForImplicitInvoke, but it is: ${context.call}" @@ -135,7 +135,7 @@ class NewResolutionOldInference( override fun createTowerProcessor( outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, scopeTower: ImplicitScopeTower, explicitReceiver: DetailedReceiver?, context: BasicCallResolutionContext - ): ScopeTowerProcessor> { + ): ScopeTowerProcessor { throw IllegalStateException("Should be not called") } } @@ -190,7 +190,7 @@ class NewResolutionOldInference( } } - val overloadResults = convertToOverloadResults(candidates, tracing, context, languageVersionSettings) + val overloadResults = convertToOverloadResults(candidates, tracing, context, languageVersionSettings) coroutineInferenceSupport.checkCoroutineCalls(context, tracing, overloadResults) return overloadResults } @@ -229,13 +229,13 @@ class NewResolutionOldInference( return convertToOverloadResults(processedCandidates, tracing, basicCallContext, languageVersionSettings) } - private fun allCandidatesResult(allCandidates: Collection>) + private fun allCandidatesResult(allCandidates: Collection) = OverloadResolutionResultsImpl.nameNotFound().apply { - this.allCandidates = allCandidates.map { it.resolvedCall } + this.allCandidates = allCandidates.map { it.resolvedCall as MutableResolvedCall } } private fun convertToOverloadResults( - candidates: Collection>, + candidates: Collection, tracing: TracingStrategy, basicCallContext: BasicCallResolutionContext, languageVersionSettings: LanguageVersionSettings @@ -269,7 +269,7 @@ class NewResolutionOldInference( } } - resolvedCall + resolvedCall as MutableResolvedCall } return resolutionResultsHandler.computeResultAndReportErrors(basicCallContext, tracing, resolvedCalls, languageVersionSettings) @@ -318,9 +318,9 @@ class NewResolutionOldInference( override val isDebuggerContext: Boolean get() = resolutionContext.isDebuggerContext } - internal data class MyCandidate( + internal data class MyCandidate( val candidateStatus: ResolutionCandidateStatus, - val resolvedCall: MutableResolvedCall<@UnsafeVariance D> + val resolvedCall: MutableResolvedCall<*> ) : Candidate { override val isSuccessful: Boolean get() = candidateStatus.resultingApplicability.isSuccess @@ -328,16 +328,16 @@ class NewResolutionOldInference( get() = candidateStatus } - private inner class CandidateFactoryImpl( + private inner class CandidateFactoryImpl( val name: Name, val basicCallContext: BasicCallResolutionContext, val tracing: TracingStrategy - ) : CandidateFactory> { + ) : CandidateFactory { override fun createCandidate( - towerCandidate: CandidateWithBoundDispatchReceiver, + towerCandidate: CandidateWithBoundDispatchReceiver, explicitReceiverKind: ExplicitReceiverKind, extensionReceiver: ReceiverValueWithSmartCastInfo? - ): MyCandidate { + ): MyCandidate { val candidateTrace = TemporaryBindingTrace.create(basicCallContext.trace, "Context for resolve candidate") val candidateCall = ResolvedCallImpl( @@ -394,16 +394,16 @@ class NewResolutionOldInference( } private inner class CandidateFactoryProviderForInvokeImpl( - val functionContext: CandidateFactoryImpl - ) : CandidateFactoryProviderForInvoke, MyCandidate> { + val functionContext: CandidateFactoryImpl + ) : CandidateFactoryProviderForInvoke { override fun transformCandidate( - variable: MyCandidate, - invoke: MyCandidate - ): MyCandidate { + variable: MyCandidate, + invoke: MyCandidate + ): MyCandidate { val resolvedCallImpl = VariableAsFunctionResolvedCallImpl( - invoke.resolvedCall, - variable.resolvedCall + invoke.resolvedCall as MutableResolvedCall, + variable.resolvedCall as MutableResolvedCall ) assert(variable.candidateStatus.resultingApplicability.isSuccess) { "Variable call must be success: $variable" @@ -412,7 +412,7 @@ class NewResolutionOldInference( return MyCandidate(ResolutionCandidateStatus(variable.candidateStatus.diagnostics + invoke.candidateStatus.diagnostics), resolvedCallImpl) } - override fun factoryForVariable(stripExplicitReceiver: Boolean): CandidateFactory> { + override fun factoryForVariable(stripExplicitReceiver: Boolean): CandidateFactory { val newCall = CallTransformer.stripCallArguments(functionContext.basicCallContext.call).let { if (stripExplicitReceiver) CallTransformer.stripReceiver(it) else it } @@ -420,15 +420,15 @@ class NewResolutionOldInference( } override fun factoryForInvoke( - variable: MyCandidate, + variable: MyCandidate, useExplicitReceiver: Boolean - ): Pair>>? { + ): Pair>? { assert(variable.resolvedCall.status.possibleTransformToSuccess()) { "Incorrect status: ${variable.resolvedCall.status} for variable call: ${variable.resolvedCall} " + "and descriptor: ${variable.resolvedCall.candidateDescriptor}" } val calleeExpression = variable.resolvedCall.call.calleeExpression - val variableDescriptor = variable.resolvedCall.resultingDescriptor + val variableDescriptor = variable.resolvedCall.resultingDescriptor as VariableDescriptor assert(variable.resolvedCall.status.possibleTransformToSuccess() && calleeExpression != null) { "Unexpected variable candidate: $variable" } @@ -452,7 +452,7 @@ class NewResolutionOldInference( .replaceCall(functionCall) .replaceContextDependency(ContextDependency.DEPENDENT) // todo - val newContext = CandidateFactoryImpl(OperatorNameConventions.INVOKE, basicCallResolutionContext, tracingForInvoke) + val newContext = CandidateFactoryImpl(OperatorNameConventions.INVOKE, basicCallResolutionContext, tracingForInvoke) return basicCallResolutionContext.transformToReceiverWithSmartCastInfo(variableReceiver) to newContext } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt index 6c1c74c20be..086b71a47fc 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt @@ -43,21 +43,21 @@ interface ImplicitScopeTower { } interface ScopeTowerLevel { - fun getVariables(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection> + fun getVariables(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection - fun getObjects(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection> + fun getObjects(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection - fun getFunctions(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection> + fun getFunctions(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection } -interface CandidateWithBoundDispatchReceiver { - val descriptor: D +interface CandidateWithBoundDispatchReceiver { + val descriptor: CallableDescriptor val diagnostics: List val dispatchReceiver: ReceiverValueWithSmartCastInfo? - fun copy(newDescriptor: @UnsafeVariance D): CandidateWithBoundDispatchReceiver + fun copy(newDescriptor: CallableDescriptor): CandidateWithBoundDispatchReceiver } data class ResolutionCandidateStatus(val diagnostics: List) { diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/InvokeProcessors.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/InvokeProcessors.kt index f68376283eb..33f612cc64f 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/InvokeProcessors.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/InvokeProcessors.kt @@ -27,16 +27,16 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastI import org.jetbrains.kotlin.util.OperatorNameConventions import java.util.* -abstract class AbstractInvokeTowerProcessor( - protected val factoryProviderForInvoke: CandidateFactoryProviderForInvoke, - private val variableProcessor: ScopeTowerProcessor -) : ScopeTowerProcessor { +abstract class AbstractInvokeTowerProcessor( + protected val factoryProviderForInvoke: CandidateFactoryProviderForInvoke, + private val variableProcessor: ScopeTowerProcessor +) : ScopeTowerProcessor { // todo optimize it private val previousData = ArrayList() private val invokeProcessors: MutableList> = ArrayList() - private inner class VariableInvokeProcessor(val variableCandidate: V): ScopeTowerProcessor { - val invokeProcessor: ScopeTowerProcessor = createInvokeProcessor(variableCandidate) + private inner class VariableInvokeProcessor(val variableCandidate: C): ScopeTowerProcessor { + val invokeProcessor: ScopeTowerProcessor = createInvokeProcessor(variableCandidate) override fun process(data: TowerData) = invokeProcessor.process(data).map { candidateGroup -> @@ -44,12 +44,12 @@ abstract class AbstractInvokeTowerProcessor( } } - protected abstract fun createInvokeProcessor(variableCandidate: V): ScopeTowerProcessor + protected abstract fun createInvokeProcessor(variableCandidate: C): ScopeTowerProcessor - override fun process(data: TowerData): List> { + override fun process(data: TowerData): List> { previousData.add(data) - val candidateGroups = ArrayList>(0) + val candidateGroups = ArrayList>(0) for (processorsGroup in invokeProcessors) { candidateGroups.addAll(processorsGroup.processVariableGroup(data)) @@ -73,7 +73,7 @@ abstract class AbstractInvokeTowerProcessor( return candidateGroups } - private fun Collection.processVariableGroup(data: TowerData): List> { + private fun Collection.processVariableGroup(data: TowerData): List> { return when (size) { 0 -> emptyList() 1 -> single().process(data) @@ -86,35 +86,35 @@ abstract class AbstractInvokeTowerProcessor( } // todo KT-9522 Allow invoke convention for synthetic property -class InvokeTowerProcessor( +class InvokeTowerProcessor( val scopeTower: ImplicitScopeTower, val name: Name, - factoryProviderForInvoke: CandidateFactoryProviderForInvoke, + factoryProviderForInvoke: CandidateFactoryProviderForInvoke, explicitReceiver: DetailedReceiver? -) : AbstractInvokeTowerProcessor( +) : AbstractInvokeTowerProcessor( factoryProviderForInvoke, createVariableAndObjectProcessor(scopeTower, name, factoryProviderForInvoke.factoryForVariable(stripExplicitReceiver = false), explicitReceiver) ) { // todo filter by operator - override fun createInvokeProcessor(variableCandidate: V): ScopeTowerProcessor { + override fun createInvokeProcessor(variableCandidate: C): ScopeTowerProcessor { val (variableReceiver, invokeContext) = factoryProviderForInvoke.factoryForInvoke(variableCandidate, useExplicitReceiver = false) ?: return KnownResultProcessor(emptyList()) return ExplicitReceiverScopeTowerProcessor(scopeTower, invokeContext, variableReceiver) { getFunctions(OperatorNameConventions.INVOKE, it) } } } -class InvokeExtensionTowerProcessor( +class InvokeExtensionTowerProcessor( val scopeTower: ImplicitScopeTower, val name: Name, - factoryProviderForInvoke: CandidateFactoryProviderForInvoke, + factoryProviderForInvoke: CandidateFactoryProviderForInvoke, private val explicitReceiver: ReceiverValueWithSmartCastInfo? -) : AbstractInvokeTowerProcessor( +) : AbstractInvokeTowerProcessor( factoryProviderForInvoke, createVariableAndObjectProcessor(scopeTower, name, factoryProviderForInvoke.factoryForVariable(stripExplicitReceiver = true), explicitReceiver = null) ) { - override fun createInvokeProcessor(variableCandidate: V): ScopeTowerProcessor { + override fun createInvokeProcessor(variableCandidate: C): ScopeTowerProcessor { val (variableReceiver, invokeContext) = factoryProviderForInvoke.factoryForInvoke(variableCandidate, useExplicitReceiver = true) ?: return KnownResultProcessor(emptyList()) val invokeDescriptor = scopeTower.getExtensionInvokeCandidateDescriptor(variableReceiver) @@ -124,10 +124,10 @@ class InvokeExtensionTowerProcessor( } private class InvokeExtensionScopeTowerProcessor( - context: CandidateFactory, - private val invokeCandidateDescriptor: CandidateWithBoundDispatchReceiver, + context: CandidateFactory, + private val invokeCandidateDescriptor: CandidateWithBoundDispatchReceiver, private val explicitReceiver: ReceiverValueWithSmartCastInfo? -) : AbstractSimpleScopeTowerProcessor(context) { +) : AbstractSimpleScopeTowerProcessor(context) { override fun simpleProcess(data: TowerData): Collection { if (explicitReceiver != null && data == TowerData.Empty) { @@ -145,7 +145,7 @@ private class InvokeExtensionScopeTowerProcessor( // todo debug info private fun ImplicitScopeTower.getExtensionInvokeCandidateDescriptor( extensionFunctionReceiver: ReceiverValueWithSmartCastInfo -): CandidateWithBoundDispatchReceiver? { +): CandidateWithBoundDispatchReceiver? { val type = extensionFunctionReceiver.receiverValue.type if (!type.isBuiltinExtensionFunctionalType) return null // todo: missing smart cast? @@ -159,12 +159,12 @@ private fun ImplicitScopeTower.getExtensionInvokeCandidateDescriptor( } // case 1.(foo())() or (foo())() -fun createCallTowerProcessorForExplicitInvoke( +fun createCallTowerProcessorForExplicitInvoke( scopeTower: ImplicitScopeTower, - functionContext: CandidateFactory, + functionContext: CandidateFactory, expressionForInvoke: ReceiverValueWithSmartCastInfo, explicitReceiver: ReceiverValueWithSmartCastInfo? -): ScopeTowerProcessor { +): ScopeTowerProcessor { val invokeExtensionDescriptor = scopeTower.getExtensionInvokeCandidateDescriptor(expressionForInvoke) if (explicitReceiver != null) { if (invokeExtensionDescriptor == null) { diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ScopeTowerProcessors.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ScopeTowerProcessors.kt index 9c32ad92084..22815d3cc61 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ScopeTowerProcessors.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ScopeTowerProcessors.kt @@ -17,8 +17,6 @@ package org.jetbrains.kotlin.resolve.calls.tower import org.jetbrains.kotlin.descriptors.CallableDescriptor -import org.jetbrains.kotlin.descriptors.FunctionDescriptor -import org.jetbrains.kotlin.descriptors.VariableDescriptor import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind import org.jetbrains.kotlin.resolve.scopes.receivers.DetailedReceiver @@ -39,8 +37,8 @@ class CompositeScopeTowerProcessor( override fun process(data: TowerData): List> = processors.flatMap { it.process(data) } } -internal abstract class AbstractSimpleScopeTowerProcessor( - val candidateFactory: CandidateFactory +internal abstract class AbstractSimpleScopeTowerProcessor( + val candidateFactory: CandidateFactory ) : ScopeTowerProcessor { protected abstract fun simpleProcess(data: TowerData): Collection @@ -48,15 +46,15 @@ internal abstract class AbstractSimpleScopeTowerProcessor> = listOfNotNull(simpleProcess(data).takeIf { it.isNotEmpty() }) } -private typealias CandidatesCollector = - ScopeTowerLevel.(extensionReceiver: ReceiverValueWithSmartCastInfo?) -> Collection> +private typealias CandidatesCollector = + ScopeTowerLevel.(extensionReceiver: ReceiverValueWithSmartCastInfo?) -> Collection -internal class ExplicitReceiverScopeTowerProcessor( +internal class ExplicitReceiverScopeTowerProcessor( val scopeTower: ImplicitScopeTower, - context: CandidateFactory, + context: CandidateFactory, val explicitReceiver: ReceiverValueWithSmartCastInfo, - val collectCandidates: CandidatesCollector -): AbstractSimpleScopeTowerProcessor(context) { + val collectCandidates: CandidatesCollector +): AbstractSimpleScopeTowerProcessor(context) { override fun simpleProcess(data: TowerData): Collection { return when (data) { TowerData.Empty -> resolveAsMember() @@ -78,12 +76,12 @@ internal class ExplicitReceiverScopeTowerProcessor( +private class QualifierScopeTowerProcessor( val scopeTower: ImplicitScopeTower, - context: CandidateFactory, + context: CandidateFactory, val qualifier: QualifierReceiver, - val collectCandidates: CandidatesCollector -): AbstractSimpleScopeTowerProcessor(context) { + val collectCandidates: CandidatesCollector +): AbstractSimpleScopeTowerProcessor(context) { override fun simpleProcess(data: TowerData): Collection { if (data != TowerData.Empty) return emptyList() @@ -94,10 +92,10 @@ private class QualifierScopeTowerProcessor } } -private class NoExplicitReceiverScopeTowerProcessor( - context: CandidateFactory, - val collectCandidates: CandidatesCollector -) : AbstractSimpleScopeTowerProcessor(context) { +private class NoExplicitReceiverScopeTowerProcessor( + context: CandidateFactory, + val collectCandidates: CandidatesCollector +) : AbstractSimpleScopeTowerProcessor(context) { override fun simpleProcess(data: TowerData): Collection = when(data) { is TowerData.TowerLevel -> { @@ -124,8 +122,8 @@ private class NoExplicitReceiverScopeTowerProcessor processCommonAndSyntheticMembers( receiverForMember: ReceiverValueWithSmartCastInfo, scopeTowerLevel: ScopeTowerLevel, - collectCandidates: CandidatesCollector, - candidateFactory: CandidateFactory, + collectCandidates: CandidatesCollector, + candidateFactory: CandidateFactory, isExplicitReceiver: Boolean ): List { val (members, syntheticExtension) = @@ -150,12 +148,12 @@ private fun processCommonAndSyntheticMem } } -private fun createSimpleProcessor( +private fun createSimpleProcessor( scopeTower: ImplicitScopeTower, - context: CandidateFactory, + context: CandidateFactory, explicitReceiver: DetailedReceiver?, classValueReceiver: Boolean, - collectCandidates: CandidatesCollector + collectCandidates: CandidatesCollector ) : ScopeTowerProcessor { if (explicitReceiver is ReceiverValueWithSmartCastInfo) { return ExplicitReceiverScopeTowerProcessor(scopeTower, context, explicitReceiver, collectCandidates) @@ -180,28 +178,28 @@ private fun createSimpleProcessor( } fun createVariableProcessor(scopeTower: ImplicitScopeTower, name: Name, - context: CandidateFactory, explicitReceiver: DetailedReceiver?, classValueReceiver: Boolean = true + context: CandidateFactory, explicitReceiver: DetailedReceiver?, classValueReceiver: Boolean = true ) = createSimpleProcessor(scopeTower, context, explicitReceiver, classValueReceiver) { getVariables(name, it) } fun createVariableAndObjectProcessor(scopeTower: ImplicitScopeTower, name: Name, - context: CandidateFactory, explicitReceiver: DetailedReceiver?, classValueReceiver: Boolean = true + context: CandidateFactory, explicitReceiver: DetailedReceiver?, classValueReceiver: Boolean = true ) = CompositeScopeTowerProcessor( createVariableProcessor(scopeTower, name, context, explicitReceiver), createSimpleProcessor(scopeTower, context, explicitReceiver, classValueReceiver) { getObjects(name, it) } ) fun createSimpleFunctionProcessor(scopeTower: ImplicitScopeTower, name: Name, - context: CandidateFactory, explicitReceiver: DetailedReceiver?, classValueReceiver: Boolean = true + context: CandidateFactory, explicitReceiver: DetailedReceiver?, classValueReceiver: Boolean = true ) = createSimpleProcessor(scopeTower, context, explicitReceiver, classValueReceiver) { getFunctions(name, it) } -fun createFunctionProcessor( +fun <С: Candidate> createFunctionProcessor( scopeTower: ImplicitScopeTower, name: Name, - simpleContext: CandidateFactory, - factoryProviderForInvoke: CandidateFactoryProviderForInvoke, + simpleContext: CandidateFactory<С>, + factoryProviderForInvoke: CandidateFactoryProviderForInvoke<С>, explicitReceiver: DetailedReceiver? -): CompositeScopeTowerProcessor { +): CompositeScopeTowerProcessor<С> { // a.foo() -- simple function call val simpleFunction = createSimpleFunctionProcessor(scopeTower, name, simpleContext, explicitReceiver) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt index 8a8242fea67..748ac67af21 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt @@ -47,12 +47,12 @@ internal abstract class AbstractScopeTowerLevel( ): ScopeTowerLevel { protected val location: LookupLocation get() = scopeTower.location - protected fun createCandidateDescriptor( - descriptor: D, + protected fun createCandidateDescriptor( + descriptor: CallableDescriptor, dispatchReceiver: ReceiverValueWithSmartCastInfo?, specialError: ResolutionDiagnostic? = null, dispatchReceiverSmartCastType: KotlinType? = null - ): CandidateWithBoundDispatchReceiver { + ): CandidateWithBoundDispatchReceiver { val diagnostics = SmartList() diagnostics.addIfNotNull(specialError) @@ -86,17 +86,17 @@ internal class MemberScopeTowerLevel( private val syntheticScopes = scopeTower.syntheticScopes - private fun collectMembers( - getMembers: ResolutionScope.(KotlinType?) -> Collection - ): Collection> { - val result = ArrayList>(0) + private fun collectMembers( + getMembers: ResolutionScope.(KotlinType?) -> Collection + ): Collection { + val result = ArrayList(0) val receiverValue = dispatchReceiver.receiverValue receiverValue.type.memberScope.getMembers(receiverValue.type).mapTo(result) { createCandidateDescriptor(it, dispatchReceiver) } val unstableError = if (dispatchReceiver.isStable) null else UnstableSmartCastDiagnostic - val unstableCandidates = if (unstableError != null) ArrayList>(0) else null + val unstableCandidates = if (unstableError != null) ArrayList(0) else null for (possibleType in dispatchReceiver.possibleTypes) { possibleType.memberScope.getMembers(possibleType).mapTo(unstableCandidates ?: result) { @@ -133,15 +133,15 @@ internal class MemberScopeTowerLevel( return ReceiverValueWithSmartCastInfo(newReceiverValue, possibleTypes, isStable) } - override fun getVariables(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection> { + override fun getVariables(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection { return collectMembers { getContributedVariables(name, location) } } - override fun getObjects(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection> { + override fun getObjects(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection { return emptyList() } - override fun getFunctions(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection> { + override fun getFunctions(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection { return collectMembers { getContributedFunctions(name, location) + it.getInnerConstructors(name, location) + syntheticScopes.collectSyntheticMemberFunctions(listOfNotNull(it), name, location) @@ -174,17 +174,17 @@ internal open class ScopeBasedTowerLevel protected constructor( internal constructor(scopeTower: ImplicitScopeTower, lexicalScope: LexicalScope) : this(scopeTower, lexicalScope as ResolutionScope) - override fun getVariables(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection> + override fun getVariables(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection = resolutionScope.getContributedVariables(name, location).map { createCandidateDescriptor(it, dispatchReceiver = null) } - override fun getObjects(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection> + override fun getObjects(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection = resolutionScope.getContributedObjectVariables(name, location).map { createCandidateDescriptor(it, dispatchReceiver = null) } - override fun getFunctions(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection> + override fun getFunctions(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection = resolutionScope.getContributedFunctionsAndConstructors(name, location, scopeTower.syntheticConstructorsProvider).map { createCandidateDescriptor(it, dispatchReceiver = null) } @@ -201,7 +201,7 @@ internal class SyntheticScopeBasedTowerLevel( private val ReceiverValueWithSmartCastInfo.allTypes: Set get() = possibleTypes + receiverValue.type - override fun getVariables(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection> { + override fun getVariables(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection { if (extensionReceiver == null) return emptyList() return syntheticScopes.collectSyntheticExtensionProperties(extensionReceiver.allTypes, name, location).map { @@ -211,15 +211,14 @@ internal class SyntheticScopeBasedTowerLevel( override fun getObjects( name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo? - ): Collection> = + ): Collection = emptyList() override fun getFunctions( name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo? - ): Collection> = + ): Collection = emptyList() - } internal class HidesMembersTowerLevel(scopeTower: ImplicitScopeTower): AbstractScopeTowerLevel(scopeTower) { @@ -227,16 +226,16 @@ internal class HidesMembersTowerLevel(scopeTower: ImplicitScopeTower): AbstractS = getCandidates(name, extensionReceiver, LexicalScope::collectVariables) override fun getObjects(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?) - = emptyList>() + = emptyList() override fun getFunctions(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?) = getCandidates(name, extensionReceiver, LexicalScope::collectFunctions) - private fun getCandidates( + private fun getCandidates( name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?, - collectCandidates: LexicalScope.(Name, LookupLocation) -> Collection - ): Collection> { + collectCandidates: LexicalScope.(Name, LookupLocation) -> Collection + ): Collection { if (extensionReceiver == null || name !in HIDES_MEMBERS_NAME_LIST) return emptyList() return scopeTower.lexicalScope.collectCandidates(name, location).filter { diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerResolver.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerResolver.kt index a522b0f7c06..9d6436072e4 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerResolver.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerResolver.kt @@ -16,9 +16,6 @@ package org.jetbrains.kotlin.resolve.calls.tower -import org.jetbrains.kotlin.descriptors.CallableDescriptor -import org.jetbrains.kotlin.descriptors.FunctionDescriptor -import org.jetbrains.kotlin.descriptors.VariableDescriptor import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind import org.jetbrains.kotlin.resolve.scopes.ImportingScope @@ -34,23 +31,23 @@ interface Candidate { val status: ResolutionCandidateStatus } -interface CandidateFactory { +interface CandidateFactory { fun createCandidate( - towerCandidate: CandidateWithBoundDispatchReceiver, + towerCandidate: CandidateWithBoundDispatchReceiver, explicitReceiverKind: ExplicitReceiverKind, extensionReceiver: ReceiverValueWithSmartCastInfo? ): C } -interface CandidateFactoryProviderForInvoke { +interface CandidateFactoryProviderForInvoke { - fun transformCandidate(variable: V, invoke: F): F + fun transformCandidate(variable: C, invoke: C): C - fun factoryForVariable(stripExplicitReceiver: Boolean): CandidateFactory + fun factoryForVariable(stripExplicitReceiver: Boolean): CandidateFactory // foo() -> ReceiverValue(foo), context for invoke // null means that there is no invoke on variable - fun factoryForInvoke(variable: V, useExplicitReceiver: Boolean): Pair>? + fun factoryForInvoke(variable: C, useExplicitReceiver: Boolean): Pair>? } sealed class TowerData { diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerUtils.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerUtils.kt index e235ec1705f..5a020ba139a 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerUtils.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerUtils.kt @@ -26,14 +26,14 @@ val ResolutionCandidateApplicability.isSuccess: Boolean val CallableDescriptor.isSynthesized: Boolean get() = (this is CallableMemberDescriptor && kind == CallableMemberDescriptor.Kind.SYNTHESIZED) -val CandidateWithBoundDispatchReceiver<*>.requiresExtensionReceiver: Boolean +val CandidateWithBoundDispatchReceiver.requiresExtensionReceiver: Boolean get() = descriptor.extensionReceiverParameter != null -internal class CandidateWithBoundDispatchReceiverImpl( +internal class CandidateWithBoundDispatchReceiverImpl( override val dispatchReceiver: ReceiverValueWithSmartCastInfo?, - override val descriptor: D, + override val descriptor: CallableDescriptor, override val diagnostics: List -) : CandidateWithBoundDispatchReceiver { - override fun copy(newDescriptor: @UnsafeVariance D) = +) : CandidateWithBoundDispatchReceiver { + override fun copy(newDescriptor: CallableDescriptor) = CandidateWithBoundDispatchReceiverImpl(dispatchReceiver, newDescriptor, diagnostics) }