Skip to content

Commit 1147139

Browse files
committed
Cleanup CandidateResolver
1 parent 76d3246 commit 1147139

File tree

1 file changed

+62
-65
lines changed

1 file changed

+62
-65
lines changed

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ class CandidateResolver(
5959
private val reflectionTypes: ReflectionTypes,
6060
private val smartCastManager: SmartCastManager
6161
) {
62-
63-
fun <D : CallableDescriptor, F : D> performResolutionForCandidateCall(
62+
fun <D : CallableDescriptor> performResolutionForCandidateCall(
6463
context: CallCandidateResolutionContext<D>,
6564
checkArguments: CheckArgumentTypesMode
6665
): Unit = with(context) {
@@ -99,9 +98,9 @@ class CandidateResolver(
9998
}
10099

101100
private fun CallCandidateResolutionContext<*>.checkValueArguments() = checkAndReport {
102-
if (call.getTypeArguments().isEmpty()
101+
if (call.typeArguments.isEmpty()
103102
&& !candidateDescriptor.typeParameters.isEmpty()
104-
&& candidateCall.getKnownTypeParametersSubstitutor() == null
103+
&& candidateCall.knownTypeParametersSubstitutor == null
105104
) {
106105
genericCandidateResolver.inferTypeArguments(this)
107106
}
@@ -111,41 +110,42 @@ class CandidateResolver(
111110
}
112111

113112
private fun CallCandidateResolutionContext<*>.processTypeArguments() = check {
114-
val jetTypeArguments = call.getTypeArguments()
113+
val ktTypeArguments = call.typeArguments
115114
if (candidateCall.knownTypeParametersSubstitutor != null) {
116115
candidateCall.setResultingSubstitutor(candidateCall.knownTypeParametersSubstitutor!!)
117116
}
118-
else if (!jetTypeArguments.isEmpty()) {
117+
else if (!ktTypeArguments.isEmpty()) {
119118
// Explicit type arguments passed
120119

121120
val typeArguments = ArrayList<KotlinType>()
122-
for (projection in jetTypeArguments) {
121+
for (projection in ktTypeArguments) {
123122
val type = projection.typeReference?.let { trace.bindingContext.get(BindingContext.TYPE, it) }
124123
?: ErrorUtils.createErrorType("Star projection in a call")
125124
typeArguments.add(type)
126125
}
127126

128127
val expectedTypeArgumentCount = candidateDescriptor.typeParameters.size
129-
for (index in jetTypeArguments.size..expectedTypeArgumentCount - 1) {
128+
for (index in ktTypeArguments.size..expectedTypeArgumentCount - 1) {
130129
typeArguments.add(ErrorUtils.createErrorType(
131-
"Explicit type argument expected for " + candidateDescriptor.typeParameters.get(index).name))
130+
"Explicit type argument expected for " + candidateDescriptor.typeParameters[index].name
131+
))
132132
}
133133
val substitution = FunctionDescriptorUtil.createSubstitution(candidateDescriptor as FunctionDescriptor, typeArguments)
134134
val substitutor = TypeSubstitutor.create(SubstitutionFilteringInternalResolveAnnotations(substitution))
135135

136-
if (expectedTypeArgumentCount != jetTypeArguments.size) {
136+
if (expectedTypeArgumentCount != ktTypeArguments.size) {
137137
candidateCall.addStatus(OTHER_ERROR)
138138
tracing.wrongNumberOfTypeArguments(trace, expectedTypeArgumentCount, candidateDescriptor)
139139
}
140140
else {
141-
checkGenericBoundsInAFunctionCall(jetTypeArguments, typeArguments, candidateDescriptor, substitutor, trace)
141+
checkGenericBoundsInAFunctionCall(ktTypeArguments, typeArguments, candidateDescriptor, substitutor, trace)
142142
}
143143

144144
candidateCall.setResultingSubstitutor(substitutor)
145145
}
146146
}
147147

148-
private fun <D : CallableDescriptor, F : D> CallCandidateResolutionContext<D>.mapArguments()
148+
private fun <D : CallableDescriptor> CallCandidateResolutionContext<D>.mapArguments()
149149
= check {
150150
val argumentMappingStatus = ValueArgumentsToParametersMapper.mapValueArgumentsToParameters(
151151
call, tracing, candidateCall, Sets.newLinkedHashSet<ValueArgument>())
@@ -154,7 +154,7 @@ class CandidateResolver(
154154
}
155155
}
156156

157-
private fun <D : CallableDescriptor, F : D> CallCandidateResolutionContext<D>.checkExpectedCallableType()
157+
private fun <D : CallableDescriptor> CallCandidateResolutionContext<D>.checkExpectedCallableType()
158158
= check {
159159
if (!noExpectedType(expectedType)) {
160160
val candidate = candidateCall.candidateDescriptor
@@ -206,15 +206,15 @@ class CandidateResolver(
206206
) == null
207207

208208
private fun CallCandidateResolutionContext<*>.checkExtensionReceiver() = checkAndReport {
209-
val receiverParameter = candidateCall.getCandidateDescriptor().extensionReceiverParameter
210-
val receiverArgument = candidateCall.getExtensionReceiver()
209+
val receiverParameter = candidateCall.candidateDescriptor.extensionReceiverParameter
210+
val receiverArgument = candidateCall.extensionReceiver
211211
if (receiverParameter != null && receiverArgument == null) {
212-
tracing.missingReceiver(candidateCall.getTrace(), receiverParameter)
212+
tracing.missingReceiver(candidateCall.trace, receiverParameter)
213213
OTHER_ERROR
214214
}
215215
else if (receiverParameter == null && receiverArgument != null) {
216-
tracing.noReceiverAllowed(candidateCall.getTrace())
217-
if (call.getCalleeExpression() is KtSimpleNameExpression) {
216+
tracing.noReceiverAllowed(candidateCall.trace)
217+
if (call.calleeExpression is KtSimpleNameExpression) {
218218
RECEIVER_PRESENCE_ERROR
219219
}
220220
else {
@@ -228,7 +228,7 @@ class CandidateResolver(
228228

229229
private fun CallCandidateResolutionContext<*>.checkDispatchReceiver() = checkAndReport {
230230
val candidateDescriptor = candidateDescriptor
231-
val dispatchReceiver = candidateCall.getDispatchReceiver()
231+
val dispatchReceiver = candidateCall.dispatchReceiver
232232
if (dispatchReceiver != null) {
233233
var nestedClass: ClassDescriptor? = null
234234
if (candidateDescriptor is ConstructorDescriptor
@@ -240,12 +240,12 @@ class CandidateResolver(
240240
nestedClass = candidateDescriptor.getReferencedDescriptor()
241241
}
242242
if (nestedClass != null) {
243-
tracing.nestedClassAccessViaInstanceReference(trace, nestedClass, candidateCall.getExplicitReceiverKind())
243+
tracing.nestedClassAccessViaInstanceReference(trace, nestedClass, candidateCall.explicitReceiverKind)
244244
return@checkAndReport OTHER_ERROR
245245
}
246246
}
247247

248-
assert((dispatchReceiver != null) == (candidateCall.getResultingDescriptor().dispatchReceiverParameter != null)) {
248+
assert((dispatchReceiver != null) == (candidateCall.resultingDescriptor.dispatchReceiverParameter != null)) {
249249
"Shouldn't happen because of TaskPrioritizer: $candidateDescriptor"
250250
}
251251

@@ -261,17 +261,17 @@ class CandidateResolver(
261261
if (!context.call.callElement.insideScript()) return true
262262

263263
// In "[email protected]()" the error will be reported on "this@Outer" instead
264-
if (context.call.getExplicitReceiver() != null || context.call.getDispatchReceiver() != null) return true
264+
if (context.call.explicitReceiver != null || context.call.dispatchReceiver != null) return true
265265

266-
val candidateThis = getDeclaringClass(context.candidateCall.getCandidateDescriptor())
266+
val candidateThis = getDeclaringClass(context.candidateCall.candidateDescriptor)
267267
if (candidateThis == null || candidateThis.kind.isSingleton) return true
268268

269-
return DescriptorResolver.checkHasOuterClassInstance(context.scope, context.trace, context.call.getCallElement(), candidateThis)
269+
return DescriptorResolver.checkHasOuterClassInstance(context.scope, context.trace, context.call.callElement, candidateThis)
270270
}
271271

272272
private fun CallCandidateResolutionContext<*>.checkAbstractAndSuper() = check {
273273
val descriptor = candidateDescriptor
274-
val expression = candidateCall.getCall().getCalleeExpression()
274+
val expression = candidateCall.call.calleeExpression
275275

276276
if (expression is KtSimpleNameExpression) {
277277
// 'B' in 'class A: B()' is JetConstructorCalleeExpression
@@ -283,7 +283,7 @@ class CandidateResolver(
283283
}
284284
}
285285

286-
val superDispatchReceiver = getReceiverSuper(candidateCall.getDispatchReceiver())
286+
val superDispatchReceiver = getReceiverSuper(candidateCall.dispatchReceiver)
287287
if (superDispatchReceiver != null) {
288288
if (descriptor is MemberDescriptor && descriptor.modality == Modality.ABSTRACT) {
289289
tracing.abstractSuperCall(trace)
@@ -293,9 +293,9 @@ class CandidateResolver(
293293

294294
// 'super' cannot be passed as an argument, for receiver arguments expression typer does not track this
295295
// See TaskPrioritizer for more
296-
val superExtensionReceiver = getReceiverSuper(candidateCall.getExtensionReceiver())
296+
val superExtensionReceiver = getReceiverSuper(candidateCall.extensionReceiver)
297297
if (superExtensionReceiver != null) {
298-
trace.report(SUPER_CANT_BE_EXTENSION_RECEIVER.on(superExtensionReceiver, superExtensionReceiver.getText()))
298+
trace.report(SUPER_CANT_BE_EXTENSION_RECEIVER.on(superExtensionReceiver, superExtensionReceiver.text))
299299
candidateCall.addStatus(OTHER_ERROR)
300300
}
301301
}
@@ -343,23 +343,19 @@ class CandidateResolver(
343343
private fun <D : CallableDescriptor, C : CallResolutionContext<C>> checkValueArgumentTypes(
344344
context: CallResolutionContext<C>,
345345
candidateCall: MutableResolvedCall<D>,
346-
resolveFunctionArgumentBodies: ResolveArgumentsMode): ValueArgumentsCheckingResult {
346+
resolveFunctionArgumentBodies: ResolveArgumentsMode
347+
): ValueArgumentsCheckingResult {
347348
var resultStatus = SUCCESS
348349
val argumentTypes = Lists.newArrayList<KotlinType>()
349-
val infoForArguments = candidateCall.getDataFlowInfoForArguments()
350-
for (entry in candidateCall.getValueArguments().entries) {
351-
val parameterDescriptor = entry.key
352-
val resolvedArgument = entry.value
353-
354-
350+
val infoForArguments = candidateCall.dataFlowInfoForArguments
351+
for ((parameterDescriptor, resolvedArgument) in candidateCall.valueArguments) {
355352
for (argument in resolvedArgument.arguments) {
356353
val expression = argument.getArgumentExpression() ?: continue
357354

358355
val expectedType = getEffectiveExpectedType(parameterDescriptor, argument)
359356

360357
val newContext = context.replaceDataFlowInfo(infoForArguments.getInfo(argument)).replaceExpectedType(expectedType)
361-
val typeInfoForCall = argumentTypeResolver.getArgumentTypeInfo(
362-
expression, newContext, resolveFunctionArgumentBodies)
358+
val typeInfoForCall = argumentTypeResolver.getArgumentTypeInfo(expression, newContext, resolveFunctionArgumentBodies)
363359
val type = typeInfoForCall.type
364360
infoForArguments.updateInfo(argument, typeInfoForCall.dataFlowInfo)
365361

@@ -403,15 +399,13 @@ class CandidateResolver(
403399
expression: KtExpression,
404400
expectedType: KotlinType,
405401
actualType: KotlinType,
406-
context: ResolutionContext<*>): KotlinType? {
402+
context: ResolutionContext<*>
403+
): KotlinType? {
407404
val receiverToCast = ExpressionReceiver.create(KtPsiUtil.safeDeparenthesize(expression), actualType, context.trace.bindingContext)
408405
val variants = smartCastManager.getSmartCastVariantsExcludingReceiver(context, receiverToCast)
409-
for (possibleType in variants) {
410-
if (KotlinTypeChecker.DEFAULT.isSubtypeOf(possibleType, expectedType)) {
411-
return possibleType
412-
}
406+
return variants.firstOrNull { possibleType ->
407+
KotlinTypeChecker.DEFAULT.isSubtypeOf(possibleType, expectedType)
413408
}
414-
return null
415409
}
416410

417411
private fun CallCandidateResolutionContext<*>.checkReceiverTypeError(): Unit = check {
@@ -421,11 +415,9 @@ class CandidateResolver(
421415
// For the expressions like '42.(f)()' where f: String.() -> Unit we'd like to generate a type mismatch error on '1',
422416
// not to throw away the candidate, so the following check is skipped.
423417
if (!isInvokeCallOnExpressionWithBothReceivers(call)) {
424-
val callExtensionReceiver = candidateCall.extensionReceiver
425-
assert(callExtensionReceiver is ReceiverValue?) { "Expected ReceiverValue, got $callExtensionReceiver" }
426-
checkReceiverTypeError(extensionReceiver, callExtensionReceiver as ReceiverValue?)
418+
checkReceiverTypeError(extensionReceiver, candidateCall.extensionReceiver)
427419
}
428-
checkReceiverTypeError(dispatchReceiver, candidateCall.getDispatchReceiver())
420+
checkReceiverTypeError(dispatchReceiver, candidateCall.dispatchReceiver)
429421
}
430422

431423
private fun CallCandidateResolutionContext<*>.checkReceiverTypeError(
@@ -454,17 +446,20 @@ class CandidateResolver(
454446

455447
resultStatus = resultStatus.combine(context.checkReceiver(
456448
candidateCall,
457-
candidateCall.getResultingDescriptor().extensionReceiverParameter,
449+
candidateCall.resultingDescriptor.extensionReceiverParameter,
458450
candidateCall.extensionReceiver,
459451
candidateCall.explicitReceiverKind.isExtensionReceiver,
460-
implicitInvokeCheck = false, isDispatchReceiver = false))
452+
implicitInvokeCheck = false, isDispatchReceiver = false
453+
))
461454

462-
resultStatus = resultStatus.combine(context.checkReceiver(candidateCall,
463-
candidateCall.getResultingDescriptor().dispatchReceiverParameter, candidateCall.getDispatchReceiver(),
464-
candidateCall.getExplicitReceiverKind().isDispatchReceiver,
455+
resultStatus = resultStatus.combine(context.checkReceiver(
456+
candidateCall,
457+
candidateCall.resultingDescriptor.dispatchReceiverParameter, candidateCall.dispatchReceiver,
458+
candidateCall.explicitReceiverKind.isDispatchReceiver,
465459
// for the invocation 'foo(1)' where foo is a variable of function type we should mark 'foo' if there is unsafe call error
466-
implicitInvokeCheck = context.call is CallForImplicitInvoke,
467-
isDispatchReceiver = true))
460+
implicitInvokeCheck = context.call is CallForImplicitInvoke,
461+
isDispatchReceiver = true
462+
))
468463

469464
if (!context.isDebuggerContext
470465
&& candidateCall.dispatchReceiver != null
@@ -571,38 +566,40 @@ class CandidateResolver(
571566
inner class ValueArgumentsCheckingResult(val status: ResolutionStatus, val argumentTypes: List<KotlinType>)
572567

573568
private fun checkGenericBoundsInAFunctionCall(
574-
jetTypeArguments: List<KtTypeProjection>,
569+
ktTypeArguments: List<KtTypeProjection>,
575570
typeArguments: List<KotlinType>,
576571
functionDescriptor: CallableDescriptor,
577572
substitutor: TypeSubstitutor,
578-
trace: BindingTrace) {
573+
trace: BindingTrace
574+
) {
579575
val typeParameters = functionDescriptor.typeParameters
580-
for (i in 0..Math.min(typeParameters.size, jetTypeArguments.size) - 1) {
581-
val typeParameterDescriptor = typeParameters.get(i)
582-
val typeArgument = typeArguments.get(i)
583-
val typeReference = jetTypeArguments.get(i).getTypeReference()
576+
for (i in 0..Math.min(typeParameters.size, ktTypeArguments.size) - 1) {
577+
val typeParameterDescriptor = typeParameters[i]
578+
val typeArgument = typeArguments[i]
579+
val typeReference = ktTypeArguments[i].typeReference
584580
if (typeReference != null) {
585581
DescriptorResolver.checkBounds(typeReference, typeArgument, typeParameterDescriptor, substitutor, trace)
586582
}
587583
}
588584
}
589585

590586
private fun <D : CallableDescriptor> CallCandidateResolutionContext<D>.shouldContinue() =
591-
candidateResolveMode == CandidateResolveMode.FULLY || candidateCall.getStatus().possibleTransformToSuccess()
587+
candidateResolveMode == CandidateResolveMode.FULLY || candidateCall.status.possibleTransformToSuccess()
592588

593589
private inline fun <D : CallableDescriptor> CallCandidateResolutionContext<D>.check(
594590
checker: CallCandidateResolutionContext<D>.() -> Unit
595591
) {
596592
if (shouldContinue()) checker()
597593
}
598594

599-
private inline fun <D : CallableDescriptor> CallCandidateResolutionContext<D>.
600-
checkAndReport(checker: CallCandidateResolutionContext<D>.() -> ResolutionStatus) {
595+
private inline fun <D : CallableDescriptor> CallCandidateResolutionContext<D>.checkAndReport(
596+
checker: CallCandidateResolutionContext<D>.() -> ResolutionStatus
597+
) {
601598
if (shouldContinue()) {
602599
candidateCall.addStatus(checker())
603600
}
604601
}
605602

606-
private val CallCandidateResolutionContext<*>.candidateDescriptor: CallableDescriptor get() = candidateCall.getCandidateDescriptor()
607-
603+
private val CallCandidateResolutionContext<*>.candidateDescriptor: CallableDescriptor
604+
get() = candidateCall.candidateDescriptor
608605
}

0 commit comments

Comments
 (0)