diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmRuntimeTypes.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmRuntimeTypes.kt index afb27ba78be..479163b0b75 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmRuntimeTypes.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmRuntimeTypes.kt @@ -14,146 +14,134 @@ * limitations under the License. */ -package org.jetbrains.kotlin.codegen; +package org.jetbrains.kotlin.codegen -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.coroutines.CoroutineUtilKt; -import org.jetbrains.kotlin.descriptors.*; -import org.jetbrains.kotlin.descriptors.annotations.Annotations; -import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor; -import org.jetbrains.kotlin.descriptors.impl.MutablePackageFragmentDescriptor; -import org.jetbrains.kotlin.incremental.components.NoLookupLocation; -import org.jetbrains.kotlin.name.FqName; -import org.jetbrains.kotlin.name.Name; -import org.jetbrains.kotlin.resolve.DescriptorUtils; -import org.jetbrains.kotlin.resolve.calls.util.FunctionTypeResolveUtilsKt; -import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt; -import org.jetbrains.kotlin.types.KotlinType; -import org.jetbrains.kotlin.types.TypeConstructorSubstitution; -import org.jetbrains.kotlin.types.TypeProjectionImpl; -import org.jetbrains.kotlin.types.Variance; -import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils; +import org.jetbrains.kotlin.coroutines.* +import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor +import org.jetbrains.kotlin.descriptors.impl.MutablePackageFragmentDescriptor +import org.jetbrains.kotlin.incremental.components.NoLookupLocation +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.resolve.calls.util.* +import org.jetbrains.kotlin.resolve.descriptorUtil.* +import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.TypeConstructorSubstitution +import org.jetbrains.kotlin.types.TypeProjectionImpl +import org.jetbrains.kotlin.types.Variance +import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils -import java.util.*; +import java.util.* -public class JvmRuntimeTypes { - private final ClassDescriptor lambda; - private final ClassDescriptor functionReference; - private final List propertyReferences; - private final List mutablePropertyReferences; - private final ClassDescriptor localVariableReference; - private final ClassDescriptor mutableLocalVariableReference; - private final KotlinType defaultContinuationSupertype; +class JvmRuntimeTypes(module: ModuleDescriptor) { + private val lambda: ClassDescriptor + private val functionReference: ClassDescriptor + private val propertyReferences: MutableList + private val mutablePropertyReferences: MutableList + private val localVariableReference: ClassDescriptor + private val mutableLocalVariableReference: ClassDescriptor + private val defaultContinuationSupertype: KotlinType - public JvmRuntimeTypes(@NotNull ModuleDescriptor module) { - PackageFragmentDescriptor kotlinJvmInternal = new MutablePackageFragmentDescriptor(module, new FqName("kotlin.jvm.internal")); + init { + val kotlinJvmInternal = MutablePackageFragmentDescriptor(module, FqName("kotlin.jvm.internal")) - this.lambda = createClass(kotlinJvmInternal, "Lambda"); - this.functionReference = createClass(kotlinJvmInternal, "FunctionReference"); - this.localVariableReference = createClass(kotlinJvmInternal, "LocalVariableReference"); - this.mutableLocalVariableReference = createClass(kotlinJvmInternal, "MutableLocalVariableReference"); - this.propertyReferences = new ArrayList(3); - this.mutablePropertyReferences = new ArrayList(3); + this.lambda = createClass(kotlinJvmInternal, "Lambda") + this.functionReference = createClass(kotlinJvmInternal, "FunctionReference") + this.localVariableReference = createClass(kotlinJvmInternal, "LocalVariableReference") + this.mutableLocalVariableReference = createClass(kotlinJvmInternal, "MutableLocalVariableReference") + this.propertyReferences = ArrayList(3) + this.mutablePropertyReferences = ArrayList(3) - for (int i = 0; i <= 2; i++) { - propertyReferences.add(createClass(kotlinJvmInternal, "PropertyReference" + i)); - mutablePropertyReferences.add(createClass(kotlinJvmInternal, "MutablePropertyReference" + i)); + for (i in 0..2) { + propertyReferences.add(createClass(kotlinJvmInternal, "PropertyReference" + i)) + mutablePropertyReferences.add(createClass(kotlinJvmInternal, "MutablePropertyReference" + i)) } - defaultContinuationSupertype = createNullableAnyContinuation(module); + defaultContinuationSupertype = createNullableAnyContinuation(module) } /** * @param module - * @return Continuation type + * * + * @return Continuation?> type */ - @NotNull - private static KotlinType createNullableAnyContinuation(@NotNull ModuleDescriptor module) { - ClassDescriptor classDescriptor = - DescriptorUtilsKt.resolveTopLevelClass( - module, DescriptorUtils.CONTINUATION_INTERFACE_FQ_NAME, NoLookupLocation.FROM_BACKEND); + private fun createNullableAnyContinuation(module: ModuleDescriptor): KotlinType { + val classDescriptor = module.resolveTopLevelClass(DescriptorUtils.CONTINUATION_INTERFACE_FQ_NAME, NoLookupLocation.FROM_BACKEND) ?: error(DescriptorUtils.CONTINUATION_INTERFACE_FQ_NAME + " was not found in built-ins") - assert classDescriptor != null : DescriptorUtils.CONTINUATION_INTERFACE_FQ_NAME + " was not found in built-ins"; - //noinspection ConstantConditions return TypeConstructorSubstitution - .createByParametersMap(Collections.singletonMap(classDescriptor.getDeclaredTypeParameters().get(0), - new TypeProjectionImpl(module.getBuiltIns().getNullableAnyType()))) - .buildSubstitutor().substitute(classDescriptor.getDefaultType(), Variance.INVARIANT); + .createByParametersMap(Collections.singletonMap(classDescriptor.declaredTypeParameters[0], + TypeProjectionImpl(module.builtIns.nullableAnyType))) + .buildSubstitutor().substitute(classDescriptor.defaultType, Variance.INVARIANT) } - @NotNull - private static ClassDescriptor createClass(@NotNull PackageFragmentDescriptor packageFragment, @NotNull String name) { - MutableClassDescriptor descriptor = new MutableClassDescriptor( + private fun createClass(packageFragment: PackageFragmentDescriptor, name: String): ClassDescriptor { + val descriptor = MutableClassDescriptor( packageFragment, ClassKind.CLASS, false, Name.identifier(name), SourceElement.NO_SOURCE - ); + ) - descriptor.setModality(Modality.FINAL); - descriptor.setVisibility(Visibilities.PUBLIC); - descriptor.setTypeParameterDescriptors(Collections.emptyList()); - descriptor.createTypeConstructor(); + descriptor.modality = Modality.FINAL + descriptor.visibility = Visibilities.PUBLIC + descriptor.setTypeParameterDescriptors(emptyList()) + descriptor.createTypeConstructor() - return descriptor; + return descriptor } - @NotNull - public Collection getSupertypesForClosure(@NotNull FunctionDescriptor descriptor) { - ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter(); + fun getSupertypesForClosure(descriptor: FunctionDescriptor): Collection { + val receiverParameter = descriptor.extensionReceiverParameter - //noinspection ConstantConditions - List parameters = descriptor.getValueParameters(); - KotlinType functionType = FunctionTypeResolveUtilsKt.createFunctionType( - DescriptorUtilsKt.getBuiltIns(descriptor), - Annotations.Companion.getEMPTY(), - receiverParameter == null ? null : receiverParameter.getType(), + + val parameters = descriptor.valueParameters + val functionType = createFunctionType( + descriptor.builtIns, + Annotations.EMPTY, + receiverParameter?.type, ExpressionTypingUtils.getValueParametersTypes(parameters), null, - descriptor.getReturnType() - ); + descriptor.returnType!! + ) - KotlinType coroutineControllerType = CoroutineUtilKt.getControllerTypeIfCoroutine(descriptor); + val coroutineControllerType = descriptor.controllerTypeIfCoroutine if (coroutineControllerType != null) { return Arrays.asList( - lambda.getDefaultType(), functionType, /*coroutineType,*/ defaultContinuationSupertype); + lambda.defaultType, functionType, /*coroutineType,*/ defaultContinuationSupertype) } - return Arrays.asList(lambda.getDefaultType(), functionType); + return Arrays.asList(lambda.defaultType, functionType) } - @NotNull - public Collection getSupertypesForFunctionReference(@NotNull FunctionDescriptor descriptor, boolean isBound) { - ReceiverParameterDescriptor extensionReceiver = descriptor.getExtensionReceiverParameter(); - ReceiverParameterDescriptor dispatchReceiver = descriptor.getDispatchReceiverParameter(); + fun getSupertypesForFunctionReference(descriptor: FunctionDescriptor, isBound: Boolean): Collection { + val extensionReceiver = descriptor.extensionReceiverParameter + val dispatchReceiver = descriptor.dispatchReceiverParameter - KotlinType receiverType = - extensionReceiver != null ? extensionReceiver.getType() : dispatchReceiver != null ? dispatchReceiver.getType() : null; + val receiverType = if (extensionReceiver != null) extensionReceiver.type else dispatchReceiver?.type - //noinspection ConstantConditions - List parameters = descriptor.getValueParameters(); - KotlinType functionType = FunctionTypeResolveUtilsKt.createFunctionType( - DescriptorUtilsKt.getBuiltIns(descriptor), - Annotations.Companion.getEMPTY(), - isBound ? null : receiverType, + + val parameters = descriptor.valueParameters + val functionType = createFunctionType( + descriptor.builtIns, + Annotations.EMPTY, + if (isBound) null else receiverType, ExpressionTypingUtils.getValueParametersTypes(parameters), null, - descriptor.getReturnType() - ); + descriptor.returnType!! + ) - return Arrays.asList(functionReference.getDefaultType(), functionType); + return Arrays.asList(functionReference.defaultType, functionType) } - @NotNull - public KotlinType getSupertypeForPropertyReference( - @NotNull VariableDescriptorWithAccessors descriptor, boolean isMutable, boolean isBound - ) { - if (descriptor instanceof LocalVariableDescriptor) { - return (isMutable ? mutableLocalVariableReference : localVariableReference).getDefaultType(); + fun getSupertypeForPropertyReference( + descriptor: VariableDescriptorWithAccessors, isMutable: Boolean, isBound: Boolean + ): KotlinType { + if (descriptor is LocalVariableDescriptor) { + return (if (isMutable) mutableLocalVariableReference else localVariableReference).defaultType } - int arity = (descriptor.getExtensionReceiverParameter() != null ? 1 : 0) + - (descriptor.getDispatchReceiverParameter() != null ? 1 : 0) - - (isBound ? 1 : 0); - return (isMutable ? mutablePropertyReferences : propertyReferences).get(arity).getDefaultType(); + val arity = (if (descriptor.extensionReceiverParameter != null) 1 else 0) + (if (descriptor.dispatchReceiverParameter != null) 1 else 0) - if (isBound) 1 else 0 + return (if (isMutable) mutablePropertyReferences else propertyReferences)[arity].defaultType } }