Refactoring. Remove supertype TowerContext from class InvokeTowerContext.

This commit is contained in:
Stanislav Erokhin
2016-07-25 20:33:16 +03:00
parent 2c2f105c5d
commit df461d6e7f
3 changed files with 27 additions and 27 deletions
@@ -72,8 +72,8 @@ class NewResolutionOldInference(
outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy,
scopeTower: ScopeTower, explicitReceiver: Receiver?, context: BasicCallResolutionContext scopeTower: ScopeTower, explicitReceiver: Receiver?, context: BasicCallResolutionContext
): ScopeTowerProcessor<MyCandidate<FunctionDescriptor>> { ): ScopeTowerProcessor<MyCandidate<FunctionDescriptor>> {
val invokeContext = outer.InvokeContext(scopeTower, name, context, tracing) val functionContext = outer.SimpleContext<FunctionDescriptor>(scopeTower, name, context, tracing)
return outer.createFunctionTowerProcessor(invokeContext, explicitReceiver) return outer.createFunctionTowerProcessor(functionContext, explicitReceiver)
} }
} }
@@ -106,13 +106,13 @@ class NewResolutionOldInference(
outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy, outer: NewResolutionOldInference, name: Name, tracing: TracingStrategy,
scopeTower: ScopeTower, explicitReceiver: Receiver?, context: BasicCallResolutionContext scopeTower: ScopeTower, explicitReceiver: Receiver?, context: BasicCallResolutionContext
): ScopeTowerProcessor<MyCandidate<FunctionDescriptor>> { ): ScopeTowerProcessor<MyCandidate<FunctionDescriptor>> {
val invokeContext = outer.InvokeContext(scopeTower, name, context, tracing) val functionContext = outer.SimpleContext<FunctionDescriptor>(scopeTower, name, context, tracing)
// todo // todo
val call = (context.call as? CallTransformer.CallForImplicitInvoke).sure { val call = (context.call as? CallTransformer.CallForImplicitInvoke).sure {
"Call should be CallForImplicitInvoke, but it is: ${context.call}" "Call should be CallForImplicitInvoke, but it is: ${context.call}"
} }
return outer.createProcessorWithReceiverValueOrEmpty(explicitReceiver) { return outer.createProcessorWithReceiverValueOrEmpty(explicitReceiver) {
createCallTowerProcessorForExplicitInvoke(invokeContext, call.dispatchReceiver, it) createCallTowerProcessorForExplicitInvoke(functionContext, call.dispatchReceiver, it)
} }
} }
@@ -203,17 +203,19 @@ class NewResolutionOldInference(
} }
private fun createFunctionTowerProcessor( private fun createFunctionTowerProcessor(
baseContext: InvokeTowerContext<MyCandidate<FunctionDescriptor>, MyCandidate<VariableDescriptor>>, simpleContext: SimpleContext<FunctionDescriptor>,
explicitReceiver: Receiver? explicitReceiver: Receiver?
): CompositeScopeTowerProcessor<MyCandidate<FunctionDescriptor>> { ): CompositeScopeTowerProcessor<MyCandidate<FunctionDescriptor>> {
val invokeContext = InvokeContext(simpleContext)
// a.foo() -- simple function call // a.foo() -- simple function call
val simpleFunction = createFunctionProcessor(baseContext, explicitReceiver) val simpleFunction = createFunctionProcessor(simpleContext, explicitReceiver)
// a.foo() -- property a.foo + foo.invoke() // a.foo() -- property a.foo + foo.invoke()
val invokeProcessor = InvokeTowerProcessor(baseContext, explicitReceiver) val invokeProcessor = InvokeTowerProcessor(invokeContext, explicitReceiver)
// a.foo() -- property foo is extension function with receiver a -- a.invoke() // a.foo() -- property foo is extension function with receiver a -- a.invoke()
val invokeExtensionProcessor = createProcessorWithReceiverValueOrEmpty(explicitReceiver) { InvokeExtensionTowerProcessor(baseContext, it) } val invokeExtensionProcessor = createProcessorWithReceiverValueOrEmpty(explicitReceiver) { InvokeExtensionTowerProcessor(invokeContext, it) }
return CompositeScopeTowerProcessor(simpleFunction, invokeProcessor, invokeExtensionProcessor) return CompositeScopeTowerProcessor(simpleFunction, invokeProcessor, invokeExtensionProcessor)
} }
@@ -274,11 +276,11 @@ class NewResolutionOldInference(
get() = candidateStatus get() = candidateStatus
} }
private inner open class SimpleContext<D : CallableDescriptor>( private inner class SimpleContext<D : CallableDescriptor>(
override val scopeTower: ScopeTower, override val scopeTower: ScopeTower,
override val name: Name, override val name: Name,
protected val basicCallContext: BasicCallResolutionContext, val basicCallContext: BasicCallResolutionContext,
protected val tracing: TracingStrategy val tracing: TracingStrategy
) : TowerContext<D, MyCandidate<D>> { ) : TowerContext<D, MyCandidate<D>> {
override fun createCandidate( override fun createCandidate(
towerCandidate: CandidateWithBoundDispatchReceiver<D>, towerCandidate: CandidateWithBoundDispatchReceiver<D>,
@@ -329,12 +331,8 @@ class NewResolutionOldInference(
} }
private inner class InvokeContext( private inner class InvokeContext(
scopeTower: ScopeTower, val functionContext: SimpleContext<FunctionDescriptor>
name: Name, ) : InvokeTowerContext<MyCandidate<FunctionDescriptor>, MyCandidate<VariableDescriptor>> {
basicCallContext: BasicCallResolutionContext,
tracing: TracingStrategy
) : InvokeTowerContext<MyCandidate<FunctionDescriptor>, MyCandidate<VariableDescriptor>>,
SimpleContext<FunctionDescriptor>(scopeTower, name, basicCallContext, tracing) {
override fun transformCandidate( override fun transformCandidate(
variable: MyCandidate<VariableDescriptor>, variable: MyCandidate<VariableDescriptor>,
@@ -352,10 +350,10 @@ class NewResolutionOldInference(
} }
override fun contextForVariable(stripExplicitReceiver: Boolean): TowerContext<VariableDescriptor, MyCandidate<VariableDescriptor>> { override fun contextForVariable(stripExplicitReceiver: Boolean): TowerContext<VariableDescriptor, MyCandidate<VariableDescriptor>> {
val newCall = CallTransformer.stripCallArguments(basicCallContext.call).let { val newCall = CallTransformer.stripCallArguments(functionContext.basicCallContext.call).let {
if (stripExplicitReceiver) CallTransformer.stripReceiver(it) else it if (stripExplicitReceiver) CallTransformer.stripReceiver(it) else it
} }
return SimpleContext(scopeTower, name, basicCallContext.replaceCall(newCall), tracing) return SimpleContext(functionContext.scopeTower, functionContext.name, functionContext.basicCallContext.replaceCall(newCall), functionContext.tracing)
} }
override fun contextForInvoke( override fun contextForInvoke(
@@ -376,11 +374,12 @@ class NewResolutionOldInference(
if (variableType is DeferredType && variableType.isComputing) { if (variableType is DeferredType && variableType.isComputing) {
return null // todo: create special check that there is no invoke on variable return null // todo: create special check that there is no invoke on variable
} }
val basicCallContext = functionContext.basicCallContext
val variableReceiver = ExpressionReceiver.create(calleeExpression!!, val variableReceiver = ExpressionReceiver.create(calleeExpression!!,
variableType, variableType,
basicCallContext.trace.bindingContext) basicCallContext.trace.bindingContext)
// used for smartCasts, see: DataFlowValueFactory.getIdForSimpleNameExpression // used for smartCasts, see: DataFlowValueFactory.getIdForSimpleNameExpression
tracing.bindReference(variable.resolvedCall.trace, variable.resolvedCall) functionContext.tracing.bindReference(variable.resolvedCall.trace, variable.resolvedCall)
// todo hacks // todo hacks
val functionCall = CallTransformer.CallForImplicitInvoke( val functionCall = CallTransformer.CallForImplicitInvoke(
basicCallContext.call.explicitReceiver?.check { useExplicitReceiver }, basicCallContext.call.explicitReceiver?.check { useExplicitReceiver },
@@ -390,6 +389,7 @@ class NewResolutionOldInference(
.replaceCall(functionCall) .replaceCall(functionCall)
.replaceContextDependency(ContextDependency.DEPENDENT) // todo .replaceContextDependency(ContextDependency.DEPENDENT) // todo
val scopeTower = functionContext.scopeTower
val newScopeTower = ScopeTowerImpl(basicCallResolutionContext, scopeTower.dynamicScope, scopeTower.syntheticScopes, scopeTower.location) val newScopeTower = ScopeTowerImpl(basicCallResolutionContext, scopeTower.dynamicScope, scopeTower.syntheticScopes, scopeTower.location)
val newContext = SimpleContext<FunctionDescriptor>(newScopeTower, OperatorNameConventions.INVOKE, basicCallResolutionContext, tracingForInvoke) val newContext = SimpleContext<FunctionDescriptor>(newScopeTower, OperatorNameConventions.INVOKE, basicCallResolutionContext, tracingForInvoke)
@@ -151,23 +151,23 @@ private fun ScopeTower.getExtensionInvokeCandidateDescriptor(
} }
// case 1.(foo())() or (foo())() // case 1.(foo())() or (foo())()
fun <F : Candidate<FunctionDescriptor>, V : Candidate<VariableDescriptor>> createCallTowerProcessorForExplicitInvoke( fun <F : Candidate<FunctionDescriptor>> createCallTowerProcessorForExplicitInvoke(
invokeContext: InvokeTowerContext<F, V>, functionContext: TowerContext<FunctionDescriptor, F>,
expressionForInvoke: ReceiverValue, expressionForInvoke: ReceiverValue,
explicitReceiver: ReceiverValue? explicitReceiver: ReceiverValue?
): ScopeTowerProcessor<F> { ): ScopeTowerProcessor<F> {
val invokeExtensionDescriptor = invokeContext.scopeTower.getExtensionInvokeCandidateDescriptor(expressionForInvoke) val invokeExtensionDescriptor = functionContext.scopeTower.getExtensionInvokeCandidateDescriptor(expressionForInvoke)
if (explicitReceiver != null) { if (explicitReceiver != null) {
if (invokeExtensionDescriptor == null) { if (invokeExtensionDescriptor == null) {
// case 1.(foo())(), where foo() isn't extension function // case 1.(foo())(), where foo() isn't extension function
return KnownResultProcessor(emptyList()) return KnownResultProcessor(emptyList())
} }
else { else {
return InvokeExtensionScopeTowerProcessor(invokeContext, invokeExtensionDescriptor, explicitReceiver = explicitReceiver) return InvokeExtensionScopeTowerProcessor(functionContext, invokeExtensionDescriptor, explicitReceiver = explicitReceiver)
} }
} }
else { else {
val usualInvoke = ExplicitReceiverScopeTowerProcessor(invokeContext, expressionForInvoke, ScopeTowerLevel::getFunctions) // todo operator val usualInvoke = ExplicitReceiverScopeTowerProcessor(functionContext, expressionForInvoke, ScopeTowerLevel::getFunctions) // todo operator
if (invokeExtensionDescriptor == null) { if (invokeExtensionDescriptor == null) {
return usualInvoke return usualInvoke
@@ -175,7 +175,7 @@ fun <F : Candidate<FunctionDescriptor>, V : Candidate<VariableDescriptor>> creat
else { else {
return CompositeScopeTowerProcessor( return CompositeScopeTowerProcessor(
usualInvoke, usualInvoke,
InvokeExtensionScopeTowerProcessor(invokeContext, invokeExtensionDescriptor, explicitReceiver = null) InvokeExtensionScopeTowerProcessor(functionContext, invokeExtensionDescriptor, explicitReceiver = null)
) )
} }
} }
@@ -49,7 +49,7 @@ interface TowerContext<D : CallableDescriptor, out C: Candidate<D>> {
): C ): C
} }
interface InvokeTowerContext<F : Candidate<FunctionDescriptor>, V : Candidate<VariableDescriptor>>: TowerContext<FunctionDescriptor, F> { interface InvokeTowerContext<F : Candidate<FunctionDescriptor>, V : Candidate<VariableDescriptor>> {
fun transformCandidate(variable: V, invoke: F): F fun transformCandidate(variable: V, invoke: F): F