From 6780a1fe90433cb90aa11a566e287c8f6f83d5b3 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Wed, 16 Sep 2015 16:39:33 +0300 Subject: [PATCH] Make DynamicCallableDescriptors non static and remove static object DynamicType --- .../jetbrains/kotlin/resolve/TypeResolver.kt | 5 ++-- .../resolve/calls/tasks/TaskPrioritizer.kt | 5 ++-- .../resolve/calls/tasks/dynamicCalls.kt | 28 ++++++++++--------- .../jetbrains/kotlin/types/dynamicTypes.kt | 17 ++++++----- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt index db57a2f2933..13b35334205 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt @@ -28,6 +28,7 @@ import org.jetbrains.kotlin.psi.codeFragmentUtil.debugTypeInfo import org.jetbrains.kotlin.psi.debugText.getDebugText import org.jetbrains.kotlin.resolve.PossiblyBareType.type import org.jetbrains.kotlin.resolve.TypeResolver.FlexibleTypeCapabilitiesProvider +import org.jetbrains.kotlin.resolve.calls.tasks.DynamicCallableDescriptors import org.jetbrains.kotlin.resolve.bindingContextUtil.recordScope import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil import org.jetbrains.kotlin.resolve.lazy.LazyEntity @@ -49,7 +50,7 @@ public class TypeResolver( private val storageManager: StorageManager, private val lazinessToken: TypeLazinessToken, private val dynamicTypesSettings: DynamicTypesSettings, - private val modifiersChecker: ModifiersChecker + private val dynamicCallableDescriptors: DynamicCallableDescriptors ) { public open class FlexibleTypeCapabilitiesProvider { @@ -243,7 +244,7 @@ public class TypeResolver( } override fun visitDynamicType(type: JetDynamicType) { - result = type(DynamicType) + result = type(dynamicCallableDescriptors.dynamicType) if (!dynamicTypesSettings.dynamicTypesAllowed) { c.trace.report(UNSUPPORTED.on(type, "Dynamic types are not supported in this context")) } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/TaskPrioritizer.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/TaskPrioritizer.kt index 072e5174388..40530931e8b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/TaskPrioritizer.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/TaskPrioritizer.kt @@ -53,7 +53,8 @@ import org.jetbrains.kotlin.types.isDynamic public class TaskPrioritizer( private val storageManager: StorageManager, - private val smartCastManager: SmartCastManager + private val smartCastManager: SmartCastManager, + private val dynamicCallableDescriptors: DynamicCallableDescriptors ) { public fun computePrioritizedTasks( @@ -234,7 +235,7 @@ public class TaskPrioritizer( addExtensionCandidates(explicitReceiver, implicitReceivers, onlyDynamicReceivers, isExplicit) c.result.addCandidates { - val dynamicScope = DynamicCallableDescriptors.createDynamicDescriptorScope(c.context.call, c.scope.ownerDescriptor) + val dynamicScope = dynamicCallableDescriptors.createDynamicDescriptorScope(c.context.call, c.scope.ownerDescriptor) val dynamicDescriptors = c.callableDescriptorCollectors.flatMap { it.getNonExtensionsByName(dynamicScope, c.name, createLookupLocation(c)) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/dynamicCalls.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/dynamicCalls.kt index 638cc38419c..2b440046c8c 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/dynamicCalls.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/dynamicCalls.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.resolve.calls.tasks +import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.impl.* @@ -31,17 +32,18 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.resolve.scopes.JetScope import org.jetbrains.kotlin.resolve.scopes.JetScopeImpl import org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver -import org.jetbrains.kotlin.types.DynamicType import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.Variance +import org.jetbrains.kotlin.types.createDynamicType import org.jetbrains.kotlin.types.expressions.OperatorConventions import org.jetbrains.kotlin.types.isDynamic import org.jetbrains.kotlin.utils.Printer -import java.util.ArrayList +import java.util.* -object DynamicCallableDescriptors { +class DynamicCallableDescriptors(private val builtIns: KotlinBuiltIns) { + + val dynamicType = createDynamicType(builtIns) - @JvmStatic fun createDynamicDescriptorScope(call: Call, owner: DeclarationDescriptor) = object : JetScopeImpl() { override fun getContainingDeclaration() = owner @@ -99,7 +101,7 @@ object DynamicCallableDescriptors { /* isConst = */ false ) propertyDescriptor.setType( - DynamicType, + dynamicType, createTypeParameters(propertyDescriptor, call), createDynamicDispatchReceiverParameter(propertyDescriptor), null: JetType? @@ -127,7 +129,7 @@ object DynamicCallableDescriptors { createDynamicDispatchReceiverParameter(functionDescriptor), createTypeParameters(functionDescriptor, call), createValueParameters(functionDescriptor, call), - DynamicType, + dynamicType, Modality.FINAL, Visibilities.PUBLIC, false, @@ -137,7 +139,7 @@ object DynamicCallableDescriptors { } private fun createDynamicDispatchReceiverParameter(owner: CallableDescriptor): ReceiverParameterDescriptorImpl { - return ReceiverParameterDescriptorImpl(owner, TransientReceiver(DynamicType)) + return ReceiverParameterDescriptorImpl(owner, TransientReceiver(dynamicType)) } private fun createTypeParameters(owner: DeclarationDescriptor, call: Call): List = call.getTypeArguments().indices.map { @@ -175,10 +177,10 @@ object DynamicCallableDescriptors { fun getFunctionType(funLiteralExpr: JetFunctionLiteralExpression): JetType { val funLiteral = funLiteralExpr.getFunctionLiteral() - val receiverType = funLiteral.getReceiverTypeReference()?.let { DynamicType } - val parameterTypes = funLiteral.getValueParameters().map { DynamicType } + val receiverType = funLiteral.getReceiverTypeReference()?.let { dynamicType } + val parameterTypes = funLiteral.getValueParameters().map { dynamicType } - return owner.builtIns.getFunctionType(Annotations.EMPTY, receiverType, parameterTypes, DynamicType) + return owner.builtIns.getFunctionType(Annotations.EMPTY, receiverType, parameterTypes, dynamicType) } for (arg in call.getValueArguments()) { @@ -196,12 +198,12 @@ object DynamicCallableDescriptors { arg.getSpreadElement() != null -> { hasSpreadOperator = true - outType = owner.builtIns.getArrayType(Variance.OUT_VARIANCE, DynamicType) - varargElementType = DynamicType + outType = owner.builtIns.getArrayType(Variance.OUT_VARIANCE, dynamicType) + varargElementType = dynamicType } else -> { - outType = DynamicType + outType = dynamicType varargElementType = null } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt index cff5ccf3365..7f3fc6cc6fb 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.types import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.types.typeUtil.builtIns open class DynamicTypesSettings { open val dynamicTypesAllowed: Boolean @@ -30,16 +31,14 @@ class DynamicTypesAllowed: DynamicTypesSettings() { interface Dynamicity : TypeCapability -// Object is created only for convenience here. Dynamic types may occur in the form of DelegatingFlexibleTypes, -// which are produced by substitutions -object DynamicType : DelegatingFlexibleType( - KotlinBuiltIns.getInstance().getNothingType(), - KotlinBuiltIns.getInstance().getNullableAnyType(), - DynamicTypeCapabilities -) - fun JetType.isDynamic(): Boolean = this.getCapability(javaClass()) != null +fun createDynamicType(builtIns: KotlinBuiltIns) = object : DelegatingFlexibleType( + builtIns.nothingType, + builtIns.nullableAnyType, + DynamicTypeCapabilities +) {} + public object DynamicTypeCapabilities : FlexibleTypeCapabilities { override val id: String get() = "kotlin.DynamicType" @@ -66,7 +65,7 @@ public object DynamicTypeCapabilities : FlexibleTypeCapabilities { override fun makeNullableAsSpecified(nullable: Boolean): JetType { // Nullability has no effect on dynamics - return DynamicType + return createDynamicType(delegateType.builtIns) } override fun computeIsNullable() = false