From 0cf7490500463c1c3098fbdd116c551a83a11f56 Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Tue, 23 Aug 2016 18:37:58 +0300 Subject: [PATCH] Moved OverloadingConflictResolver into resolution module. --- .../resolve/calls/results/FlatSignature.kt | 50 --------- ...ity.kt => FlatSignatureForResolvedCall.kt} | 57 +--------- .../results/ResolutionResultsHandler.java | 2 +- compiler/resolution/resolution.iml | 1 + .../calls/context/CheckArgumentTypesMode.java | 2 +- .../resolve/calls/results/FlatSignature.kt | 106 ++++++++++++++++++ .../results/OverloadingConflictResolver.kt | 0 7 files changed, 110 insertions(+), 108 deletions(-) delete mode 100644 compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt rename compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/{FlatSignatureSpecificity.kt => FlatSignatureForResolvedCall.kt} (59%) rename compiler/{frontend => resolution}/src/org/jetbrains/kotlin/resolve/calls/context/CheckArgumentTypesMode.java (95%) create mode 100644 compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt rename compiler/{frontend => resolution}/src/org/jetbrains/kotlin/resolve/calls/results/OverloadingConflictResolver.kt (100%) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt deleted file mode 100644 index d877ad645c1..00000000000 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.descriptors.CallableDescriptor -import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor -import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor -import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.utils.singletonOrEmptyList - -class FlatSignature( - val origin: T, - val typeParameters: Collection, - val valueParameterTypes: List, - val hasExtensionReceiver: Boolean, - val hasVarargs: Boolean, - val numDefaults: Int -) { - val isGeneric = typeParameters.isNotEmpty() - - companion object { - fun createFromCallableDescriptor(descriptor: D): FlatSignature = - FlatSignature(descriptor, - descriptor.typeParameters, - valueParameterTypes = descriptor.extensionReceiverTypeOrEmpty() + descriptor.valueParameters.map { it.argumentValueType }, - hasExtensionReceiver = descriptor.extensionReceiverParameter != null, - hasVarargs = descriptor.valueParameters.any { it.varargElementType != null }, - numDefaults = 0) - - val ValueParameterDescriptor.argumentValueType: KotlinType - get() = varargElementType ?: type - - fun CallableDescriptor.extensionReceiverTypeOrEmpty() = - extensionReceiverParameter?.type.singletonOrEmptyList() - } -} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignatureSpecificity.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignatureForResolvedCall.kt similarity index 59% rename from compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignatureSpecificity.kt rename to compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignatureForResolvedCall.kt index 43a1d369560..f7e9ffd5aea 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignatureSpecificity.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignatureForResolvedCall.kt @@ -17,7 +17,6 @@ package org.jetbrains.kotlin.resolve.calls.results import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.psi.ValueArgument import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilderImpl @@ -27,63 +26,9 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCallImpl import org.jetbrains.kotlin.resolve.calls.results.FlatSignature.Companion.argumentValueType import org.jetbrains.kotlin.resolve.calls.results.FlatSignature.Companion.extensionReceiverTypeOrEmpty -import org.jetbrains.kotlin.types.* -import org.jetbrains.kotlin.types.checker.KotlinTypeChecker +import org.jetbrains.kotlin.types.KotlinType import java.util.* -interface SpecificityComparisonCallbacks { - fun isNonSubtypeNotLessSpecific(specific: KotlinType, general: KotlinType): Boolean -} - -interface TypeSpecificityComparator { - fun isDefinitelyLessSpecific(specific: KotlinType, general: KotlinType): Boolean - - object NONE: TypeSpecificityComparator { - override fun isDefinitelyLessSpecific(specific: KotlinType, general: KotlinType) = false - } -} - -interface SimpleConstraintSystem { - fun registerTypeVariables(typeParameters: Collection): TypeSubstitutor - fun addSubtypeConstraint(subType: UnwrappedType, superType: UnwrappedType) - fun hasContradiction(): Boolean -} - -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() || !TypeUtils.dependsOnTypeParameters(generalType, typeParameters)) { - if (!KotlinTypeChecker.DEFAULT.isSubtypeOf(specificType, generalType)) { - if (!callbacks.isNonSubtypeNotLessSpecific(specificType, generalType)) { - return false - } - } - } - else { - val substitutedGeneralType = typeSubstitutor.safeSubstitute(generalType, Variance.INVARIANT) - addSubtypeConstraint(specificType.unwrap(), substitutedGeneralType.unwrap()) - } - } - - return !hasContradiction() -} - fun > RC.createFlatSignature(): FlatSignature { val originalDescriptor = candidateDescriptor.original diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionResultsHandler.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionResultsHandler.java index f2d854f5f6a..48a68391615 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionResultsHandler.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/ResolutionResultsHandler.java @@ -41,7 +41,7 @@ public class ResolutionResultsHandler { @NotNull KotlinBuiltIns builtIns, @NotNull TypeSpecificityComparator specificityComparator ) { - overloadingConflictResolver = FlatSignatureSpecificityKt.createOverloadingConflictResolver(builtIns, specificityComparator); + overloadingConflictResolver = FlatSignatureForResolvedCallKt.createOverloadingConflictResolver(builtIns, specificityComparator); } @NotNull diff --git a/compiler/resolution/resolution.iml b/compiler/resolution/resolution.iml index 90f20cf2d7c..8d7cc04c5a9 100644 --- a/compiler/resolution/resolution.iml +++ b/compiler/resolution/resolution.iml @@ -9,5 +9,6 @@ + \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/context/CheckArgumentTypesMode.java b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/context/CheckArgumentTypesMode.java similarity index 95% rename from compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/context/CheckArgumentTypesMode.java rename to compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/context/CheckArgumentTypesMode.java index b6215c24fd2..b79cc86bfa5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/context/CheckArgumentTypesMode.java +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/context/CheckArgumentTypesMode.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * 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. diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt new file mode 100644 index 00000000000..4cddb870b26 --- /dev/null +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/results/FlatSignature.kt @@ -0,0 +1,106 @@ +/* + * 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.descriptors.CallableDescriptor +import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor +import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor +import org.jetbrains.kotlin.types.* +import org.jetbrains.kotlin.types.checker.KotlinTypeChecker +import org.jetbrains.kotlin.utils.singletonOrEmptyList + +interface SpecificityComparisonCallbacks { + fun isNonSubtypeNotLessSpecific(specific: KotlinType, general: KotlinType): Boolean +} + +interface TypeSpecificityComparator { + fun isDefinitelyLessSpecific(specific: KotlinType, general: KotlinType): Boolean + + object NONE: TypeSpecificityComparator { + override fun isDefinitelyLessSpecific(specific: KotlinType, general: KotlinType) = false + } +} + +class FlatSignature( + val origin: T, + val typeParameters: Collection, + val valueParameterTypes: List, + val hasExtensionReceiver: Boolean, + val hasVarargs: Boolean, + val numDefaults: Int +) { + val isGeneric = typeParameters.isNotEmpty() + + companion object { + fun createFromCallableDescriptor(descriptor: D): FlatSignature = + FlatSignature(descriptor, + descriptor.typeParameters, + valueParameterTypes = descriptor.extensionReceiverTypeOrEmpty() + descriptor.valueParameters.map { it.argumentValueType }, + hasExtensionReceiver = descriptor.extensionReceiverParameter != null, + hasVarargs = descriptor.valueParameters.any { it.varargElementType != null }, + numDefaults = 0) + + val ValueParameterDescriptor.argumentValueType: KotlinType + get() = varargElementType ?: type + + fun CallableDescriptor.extensionReceiverTypeOrEmpty() = + extensionReceiverParameter?.type.singletonOrEmptyList() + } +} + + +interface SimpleConstraintSystem { + fun registerTypeVariables(typeParameters: Collection): TypeSubstitutor + fun addSubtypeConstraint(subType: UnwrappedType, superType: UnwrappedType) + fun hasContradiction(): Boolean +} + +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() || !TypeUtils.dependsOnTypeParameters(generalType, typeParameters)) { + if (!KotlinTypeChecker.DEFAULT.isSubtypeOf(specificType, generalType)) { + if (!callbacks.isNonSubtypeNotLessSpecific(specificType, generalType)) { + return false + } + } + } + else { + val substitutedGeneralType = typeSubstitutor.safeSubstitute(generalType, Variance.INVARIANT) + addSubtypeConstraint(specificType.unwrap(), substitutedGeneralType.unwrap()) + } + } + + return !hasContradiction() +} + diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/OverloadingConflictResolver.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/results/OverloadingConflictResolver.kt similarity index 100% rename from compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/results/OverloadingConflictResolver.kt rename to compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/results/OverloadingConflictResolver.kt