
org.jetbrains.kotlin.resolve.calls.results.FlatSignature.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.resolve.calls.results
import org.jetbrains.kotlin.container.DefaultImplementation
import org.jetbrains.kotlin.container.PlatformExtensionsClashResolver
import org.jetbrains.kotlin.container.PlatformSpecificExtension
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.MemberDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.synthetic.SyntheticMemberDescriptor
import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.model.*
interface SpecificityComparisonCallbacks {
fun isNonSubtypeNotLessSpecific(specific: KotlinTypeMarker, general: KotlinTypeMarker): Boolean
}
@DefaultImplementation(impl = TypeSpecificityComparator.NONE::class)
interface TypeSpecificityComparator : PlatformSpecificExtension {
fun isDefinitelyLessSpecific(specific: KotlinTypeMarker, general: KotlinTypeMarker): Boolean
object NONE : TypeSpecificityComparator {
override fun isDefinitelyLessSpecific(specific: KotlinTypeMarker, general: KotlinTypeMarker) = false
}
}
class FlatSignature constructor(
val origin: T,
val typeParameters: Collection,
val valueParameterTypes: List,
val hasExtensionReceiver: Boolean,
val hasVarargs: Boolean,
val numDefaults: Int,
val isExpect: Boolean,
val isSyntheticMember: Boolean
) {
val isGeneric = typeParameters.isNotEmpty()
companion object {
fun createFromReflectionType(
origin: T,
descriptor: CallableDescriptor,
numDefaults: Int,
reflectionType: UnwrappedType
): FlatSignature {
return FlatSignature(
origin,
descriptor.typeParameters,
reflectionType.arguments.map { it.type }, // should we drop return type?
hasExtensionReceiver = false,
hasVarargs = descriptor.valueParameters.any { it.varargElementType != null },
numDefaults = numDefaults,
isExpect = descriptor is MemberDescriptor && descriptor.isExpect,
isSyntheticMember = descriptor is SyntheticMemberDescriptor<*>
)
}
fun create(
origin: T,
descriptor: CallableDescriptor,
numDefaults: Int,
parameterTypes: List
): FlatSignature {
val extensionReceiverType = descriptor.extensionReceiverParameter?.type
return FlatSignature(
origin,
descriptor.typeParameters,
valueParameterTypes =
listOfNotNull(extensionReceiverType) + parameterTypes,
hasExtensionReceiver = extensionReceiverType != null,
hasVarargs = descriptor.valueParameters.any { it.varargElementType != null },
numDefaults = numDefaults,
isExpect = descriptor is MemberDescriptor && descriptor.isExpect,
isSyntheticMember = descriptor is SyntheticMemberDescriptor<*>
)
}
fun createFromCallableDescriptor(
descriptor: D
): FlatSignature =
create(descriptor, descriptor, numDefaults = 0, parameterTypes = descriptor.valueParameters.map { it.argumentValueType })
fun createForPossiblyShadowedExtension(descriptor: D): FlatSignature =
FlatSignature(
descriptor,
descriptor.typeParameters,
valueParameterTypes = descriptor.valueParameters.map { it.argumentValueType },
hasExtensionReceiver = false,
hasVarargs = descriptor.valueParameters.any { it.varargElementType != null },
numDefaults = descriptor.valueParameters.count { it.hasDefaultValue() },
isExpect = descriptor is MemberDescriptor && descriptor.isExpect,
isSyntheticMember = descriptor is SyntheticMemberDescriptor<*>
)
val ValueParameterDescriptor.argumentValueType get() = varargElementType ?: type
}
}
interface SimpleConstraintSystem {
fun registerTypeVariables(typeParameters: Collection): TypeSubstitutorMarker
fun addSubtypeConstraint(subType: KotlinTypeMarker, superType: KotlinTypeMarker)
fun hasContradiction(): Boolean
// todo hack for migration
val captureFromArgument get() = false
val context: TypeSystemInferenceExtensionContext
}
fun SimpleConstraintSystem.isSignatureNotLessSpecific(
specific: FlatSignature,
general: FlatSignature,
callbacks: SpecificityComparisonCallbacks,
specificityComparator: TypeSpecificityComparator
): Boolean {
if (specific.hasExtensionReceiver != general.hasExtensionReceiver) return false
if (specific.valueParameterTypes.size != general.valueParameterTypes.size) return false
val typeParameters = general.typeParameters
val typeSubstitutor = registerTypeVariables(typeParameters)
for ((specificType, generalType) in specific.valueParameterTypes.zip(general.valueParameterTypes)) {
if (specificType == null || generalType == null) continue
if (specificityComparator.isDefinitelyLessSpecific(specificType, generalType)) {
return false
}
if (typeParameters.isEmpty() || !generalType.dependsOnTypeParameters(context, typeParameters)) {
if (!AbstractTypeChecker.isSubtypeOf(context, specificType, generalType)) {
if (!callbacks.isNonSubtypeNotLessSpecific(specificType, generalType)) {
return false
}
}
} else {
val substitutedGeneralType = typeSubstitutor.safeSubstitute(context, generalType)
/**
* Example:
* fun Array.sort(): Unit {}
* fun > Array.sort(): Unit {}
* Here, when we try solve this CS(Y is variables) then Array <: Array and this system impossible to solve,
* so we capture types from receiver and value parameters.
*/
val specificCapturedType =
context.prepareType(specificType).let { if (captureFromArgument) context.captureFromExpression(it) ?: it else it }
addSubtypeConstraint(specificCapturedType, substitutedGeneralType)
}
}
return !hasContradiction()
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy