Resolve type alias SAM constructors in synthetic scope

This commit is contained in:
Mikhail Zarechenskiy
2017-05-04 03:11:40 +03:00
parent 7541a3754d
commit e821b25288
10 changed files with 43 additions and 130 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 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.
@@ -25,7 +25,6 @@ import org.jetbrains.kotlin.resolve.calls.checkers.ReifiedTypeParameterSubstitut
import org.jetbrains.kotlin.resolve.checkers.HeaderImplDeclarationChecker
import org.jetbrains.kotlin.resolve.jvm.*
import org.jetbrains.kotlin.resolve.jvm.checkers.*
import org.jetbrains.kotlin.synthetic.JavaSyntheticConstructorsProvider
import org.jetbrains.kotlin.synthetic.JavaSyntheticScopes
import org.jetbrains.kotlin.types.DynamicTypesSettings
@@ -86,7 +85,6 @@ object JvmPlatformConfigurator : PlatformConfigurator(
container.useImpl<JavaSyntheticScopes>()
container.useImpl<InterfaceDefaultMethodCallChecker>()
container.useImpl<InlinePlatformCompatibilityChecker>()
container.useInstance(JavaSyntheticConstructorsProvider)
container.useInstance(JvmTypeSpecificityComparator)
}
}
@@ -1,56 +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.synthetic
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptorImpl
import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaClassDescriptor
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils
import org.jetbrains.kotlin.resolve.scopes.SyntheticConstructorsProvider
import java.lang.AssertionError
object JavaSyntheticConstructorsProvider : SyntheticConstructorsProvider {
override fun getSyntheticConstructors(classifier: ClassifierDescriptor, location: LookupLocation): Collection<FunctionDescriptor> {
if (classifier is TypeAliasDescriptor) {
return getSyntheticTypeAliasConstructors(classifier, location)
}
return emptyList()
}
private fun getSyntheticTypeAliasConstructors(typeAliasDescriptor: TypeAliasDescriptor, location: LookupLocation): Collection<FunctionDescriptor> {
val classDescriptor = typeAliasDescriptor.classDescriptor
if (classDescriptor !is LazyJavaClassDescriptor || classDescriptor.functionTypeForSamInterface == null) return emptyList()
val containingDeclaration = classDescriptor.containingDeclaration
val outerScope = when (containingDeclaration) {
is ClassDescriptor ->
containingDeclaration.staticScope
is PackageFragmentDescriptor ->
containingDeclaration.getMemberScope()
else ->
throw AssertionError("Unexpected containing declaration for $classDescriptor: $containingDeclaration")
}
return outerScope.getContributedFunctions(classDescriptor.name, location)
.filterIsInstance<SamConstructorDescriptorImpl>()
.filter { it.baseDescriptorForSynthetic == classDescriptor }
.map { SingleAbstractMethodUtils.createTypeAliasSamConstructorFunction(typeAliasDescriptor, it) }
}
}
@@ -133,12 +133,33 @@ class SamAdapterFunctionsScope(
override fun getSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>): Collection<PropertyDescriptor> = emptyList()
override fun getSyntheticStaticFunctions(scope: ResolutionScope, name: Name, location: LookupLocation): Collection<FunctionDescriptor> {
val samConstructor = scope.getContributedClassifier(name, location)?.let { samConstructorForClassifier(it) }
val classifier = scope.getContributedClassifier(name, location)
val samConstructor = classifier?.let { getSamConstructor(it) }
return scope.getContributedFunctions(name, location).mapNotNull { samAdapterForStaticFunction(it) } + listOfNotNull(samConstructor)
}
override fun getSyntheticStaticFunctions(scope: ResolutionScope): Collection<FunctionDescriptor> {
return scope.getContributedDescriptors(DescriptorKindFilter.FUNCTIONS).mapNotNull { samAdapterForStaticFunction(it) }
val samConstructors =
scope.getContributedDescriptors(DescriptorKindFilter.CLASSIFIERS)
.filterIsInstance<ClassifierDescriptor>()
.mapNotNull{ getSamConstructor(it) }
return scope.getContributedDescriptors(DescriptorKindFilter.FUNCTIONS).mapNotNull { samAdapterForStaticFunction(it) } +
samConstructors
}
private fun getSamConstructor(classifier: ClassifierDescriptor): SamConstructorDescriptor? {
return when (classifier) {
is TypeAliasDescriptor -> getTypeAliasSamConstructor(classifier)
else -> samConstructorForClassifier(classifier)
}
}
private fun getTypeAliasSamConstructor(classifier: TypeAliasDescriptor): SamConstructorDescriptor? {
val classDescriptor = classifier.classDescriptor ?: return null
val samConstructor = samConstructorForClassifier(classDescriptor) ?: return null
return SingleAbstractMethodUtils.createTypeAliasSamConstructorFunction(classifier, samConstructor)
}
private class MyFunctionDescriptor(
@@ -25,7 +25,6 @@ import org.jetbrains.kotlin.resolve.calls.checkers.*
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
import org.jetbrains.kotlin.resolve.checkers.*
import org.jetbrains.kotlin.resolve.lazy.DelegationFilter
import org.jetbrains.kotlin.resolve.scopes.SyntheticConstructorsProvider
import org.jetbrains.kotlin.resolve.scopes.SyntheticScopes
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.types.DynamicTypesSettings
@@ -70,7 +69,6 @@ abstract class TargetPlatform(val platformName: String) {
) {
override fun configureModuleComponents(container: StorageComponentContainer) {
container.useInstance(SyntheticScopes.Empty)
container.useInstance(SyntheticConstructorsProvider.Empty)
container.useInstance(TypeSpecificityComparator.NONE)
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 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.
@@ -48,7 +48,6 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.hasDynamicExtensionAnnotation
import org.jetbrains.kotlin.resolve.isHiddenInResolution
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.resolve.scopes.SyntheticConstructorsProvider
import org.jetbrains.kotlin.resolve.scopes.SyntheticScopes
import org.jetbrains.kotlin.resolve.scopes.receivers.*
import org.jetbrains.kotlin.types.DeferredType
@@ -66,7 +65,6 @@ class NewResolutionOldInference(
private val resolutionResultsHandler: ResolutionResultsHandler,
private val dynamicCallableDescriptors: DynamicCallableDescriptors,
private val syntheticScopes: SyntheticScopes,
private val syntheticConstructorsProvider: SyntheticConstructorsProvider,
private val languageVersionSettings: LanguageVersionSettings,
private val coroutineInferenceSupport: CoroutineInferenceSupport
) {
@@ -156,7 +154,7 @@ class NewResolutionOldInference(
}
val dynamicScope = dynamicCallableDescriptors.createDynamicDescriptorScope(context.call, context.scope.ownerDescriptor)
val scopeTower = ImplicitScopeTowerImpl(context, dynamicScope, syntheticScopes, syntheticConstructorsProvider, context.call.createLookupLocation())
val scopeTower = ImplicitScopeTowerImpl(context, dynamicScope, syntheticScopes, context.call.createLookupLocation())
val shouldUseOperatorRem = languageVersionSettings.supportsFeature(LanguageFeature.OperatorRem)
val isBinaryRemOperator = isBinaryRemOperator(context.call)
@@ -303,7 +301,6 @@ class NewResolutionOldInference(
val resolutionContext: ResolutionContext<*>,
override val dynamicScope: MemberScope,
override val syntheticScopes: SyntheticScopes,
override val syntheticConstructorsProvider: SyntheticConstructorsProvider,
override val location: LookupLocation
): ImplicitScopeTower {
private val cache = HashMap<ReceiverValue, ReceiverValueWithSmartCastInfo>()
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 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.
@@ -16,12 +16,13 @@
package org.jetbrains.kotlin.resolve.calls.tower
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.resolve.scopes.SyntheticConstructorsProvider
import org.jetbrains.kotlin.resolve.scopes.SyntheticScopes
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastInfo
import org.jetbrains.kotlin.types.KotlinType
@@ -35,8 +36,6 @@ interface ImplicitScopeTower {
val syntheticScopes: SyntheticScopes
val syntheticConstructorsProvider: SyntheticConstructorsProvider
val location: LookupLocation
val isDebuggerContext: Boolean
@@ -163,7 +163,6 @@ internal class QualifierScopeTowerLevel(scopeTower: ImplicitScopeTower, val qual
override fun getFunctions(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?) = qualifier.staticScope
.getContributedFunctionsAndConstructors(name,
location,
scopeTower.syntheticConstructorsProvider,
scopeTower.syntheticScopes,
qualifier.staticScope).map {
createCandidateDescriptor(it, dispatchReceiver = null)
@@ -191,7 +190,6 @@ internal open class ScopeBasedTowerLevel protected constructor(
override fun getFunctions(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection<CandidateWithBoundDispatchReceiver>
= resolutionScope.getContributedFunctionsAndConstructors(name,
location,
scopeTower.syntheticConstructorsProvider,
scopeTower.syntheticScopes,
resolutionScope).map {
createCandidateDescriptor(it, dispatchReceiver = null)
@@ -271,30 +269,25 @@ private fun KotlinType?.getInnerConstructors(name: Name, location: LookupLocatio
private fun ResolutionScope.getContributedFunctionsAndConstructors(
name: Name,
location: LookupLocation,
syntheticConstructorsProvider: SyntheticConstructorsProvider,
syntheticScopes: SyntheticScopes,
scope: ResolutionScope
): Collection<FunctionDescriptor> {
val result = ArrayList<FunctionDescriptor>(getContributedFunctions(name, location))
val classifier = getContributedClassifier(name, location)
if (classifier != null) {
classifier.getCallableConstructors().filterTo(result) { it.dispatchReceiverParameter == null }
syntheticConstructorsProvider.getSyntheticConstructors(classifier, location).filterTo(result) { it.dispatchReceiverParameter == null }
val callableConstructors = when (classifier) {
is TypeAliasDescriptor -> if (classifier.canHaveCallableConstructors) classifier.constructors else emptyList()
is ClassDescriptor -> if (classifier.canHaveCallableConstructors) classifier.constructors else emptyList()
else -> emptyList()
}
callableConstructors.filterTo(result) { it.dispatchReceiverParameter == null }
result.addAll(syntheticScopes.collectSyntheticStaticFunctions(scope, name, location))
return result.toList()
}
private fun ClassifierDescriptor.getCallableConstructors(): Collection<FunctionDescriptor> =
when (this) {
is TypeAliasDescriptor -> if (canHaveCallableConstructors) constructors else emptyList()
is ClassDescriptor -> if (canHaveCallableConstructors) constructors else emptyList()
else -> emptyList()
}
private fun ResolutionScope.getContributedObjectVariables(name: Name, location: LookupLocation): Collection<VariableDescriptor> {
val objectDescriptor = getFakeDescriptorForObject(getContributedClassifier(name, location))
@@ -1,30 +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.scopes
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.incremental.components.LookupLocation
interface SyntheticConstructorsProvider {
fun getSyntheticConstructors(classifier: ClassifierDescriptor, location: LookupLocation): Collection<FunctionDescriptor>
object Empty : SyntheticConstructorsProvider {
override fun getSyntheticConstructors(classifier: ClassifierDescriptor, location: LookupLocation): Collection<FunctionDescriptor> =
emptyList()
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 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.
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.idea.codeInsight.collectSyntheticStaticMembers
import org.jetbrains.kotlin.idea.completion.*
import org.jetbrains.kotlin.idea.completion.handlers.KotlinFunctionInsertHandler
import org.jetbrains.kotlin.idea.core.ExpectedInfo
@@ -42,14 +43,13 @@ import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
import org.jetbrains.kotlin.idea.util.*
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor
import org.jetbrains.kotlin.load.java.descriptors.SamTypeAliasConstructorDescriptor
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.resolveTopLevelClass
import org.jetbrains.kotlin.synthetic.JavaSyntheticConstructorsProvider
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.util.constructors
import org.jetbrains.kotlin.util.kind
@@ -305,19 +305,14 @@ class TypeInstantiationItems(
classDescriptor: ClassDescriptor?,
tail: Tail?) {
if (classDescriptor?.kind == ClassKind.INTERFACE) {
val samConstructor = if (classifier is TypeAliasDescriptor) {
JavaSyntheticConstructorsProvider.getSyntheticConstructors(classifier, NoLookupLocation.FROM_IDE)
.filterIsInstance<SamTypeAliasConstructorDescriptor>()
.singleOrNull() ?: return
}
else {
val samConstructor = run {
val container = classifier.containingDeclaration
val scope = when (container) {
is PackageFragmentDescriptor -> container.getMemberScope()
is ClassDescriptor -> container.staticScope
is ClassDescriptor -> container.unsubstitutedMemberScope
else -> return
}
scope.getContributedFunctions(classifier.name, NoLookupLocation.FROM_IDE)
scope.collectSyntheticStaticMembers(resolutionFacade, DescriptorKindFilter.FUNCTIONS, { classifier.name == it })
.filterIsInstance<SamConstructorDescriptor>()
.singleOrNull() ?: return
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 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.
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.resolve.PlatformConfigurator
import org.jetbrains.kotlin.resolve.calls.checkers.ReifiedTypeParameterSubstitutionChecker
import org.jetbrains.kotlin.resolve.checkers.HeaderImplDeclarationChecker
import org.jetbrains.kotlin.resolve.lazy.DelegationFilter
import org.jetbrains.kotlin.resolve.scopes.SyntheticConstructorsProvider
import org.jetbrains.kotlin.resolve.scopes.SyntheticScopes
import org.jetbrains.kotlin.types.DynamicTypesAllowed
@@ -60,7 +59,6 @@ object JsPlatformConfigurator : PlatformConfigurator(
override fun configureModuleComponents(container: StorageComponentContainer) {
container.useImpl<JsCallChecker>()
container.useInstance(SyntheticScopes.Empty)
container.useInstance(SyntheticConstructorsProvider.Empty)
container.useInstance(JsTypeSpecificityComparator)
container.useInstance(JsNameClashChecker())
container.useInstance(JsNameCharsChecker())