diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt index d42f0b47d92..d4076b8e2cc 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt @@ -241,7 +241,7 @@ class IrExpressionLambdaImpl( override val invokeMethodDescriptor: FunctionDescriptor = // Need the descriptor without captured parameters here. - (function.descriptor as? WrappedSimpleFunctionDescriptor)?.originalDescriptor ?: function.descriptor + function.originalFunction.descriptor override val hasDispatchReceiver: Boolean = false diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBasedDescriptors.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBasedDescriptors.kt new file mode 100644 index 00000000000..6d1b22f3de7 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBasedDescriptors.kt @@ -0,0 +1,1094 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.ir.descriptors + +import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor +import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl +import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor +import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI +import org.jetbrains.kotlin.ir.IrElement +import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyFunction +import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyProperty +import org.jetbrains.kotlin.ir.expressions.* +import org.jetbrains.kotlin.ir.symbols.IrClassSymbol +import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol +import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol +import org.jetbrains.kotlin.ir.types.* +import org.jetbrains.kotlin.ir.util.* +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.constants.* +import org.jetbrains.kotlin.resolve.descriptorUtil.classId +import org.jetbrains.kotlin.resolve.descriptorUtil.module +import org.jetbrains.kotlin.resolve.scopes.LazyScopeAdapter +import org.jetbrains.kotlin.resolve.scopes.MemberScope +import org.jetbrains.kotlin.resolve.scopes.TypeIntersectionScope +import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue +import org.jetbrains.kotlin.serialization.deserialization.descriptors.DescriptorWithContainerSource +import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource +import org.jetbrains.kotlin.storage.LockBasedStorageManager +import org.jetbrains.kotlin.types.* + +/* Descriptors that serve purely as a view into IR structures. + Created each time at the borderline between IR-based and descriptor-based code (such as inliner). + Compared to WrappedDescriptors, no method calls ever return true descriptors. + */ + +abstract class IrBasedDeclarationDescriptor(val owner: T) : DeclarationDescriptor { + override val annotations: Annotations by lazy { + val ownerAnnotations = (owner as? IrAnnotationContainer)?.annotations ?: return@lazy Annotations.EMPTY + Annotations.create(ownerAnnotations.map { it.toAnnotationDescriptor() }) + } + + private fun IrConstructorCall.toAnnotationDescriptor(): AnnotationDescriptor { + assert(symbol.owner.parentAsClass.isAnnotationClass) { + "Expected call to constructor of annotation class but was: ${this.dump()}" + } + return AnnotationDescriptorImpl( + symbol.owner.parentAsClass.defaultType.toIrBasedKotlinType(), + symbol.owner.valueParameters.map { it.name to getValueArgument(it.index) } + .filter { it.second != null } + .associate { it.first to it.second!!.toConstantValue() }, + /*TODO*/ SourceElement.NO_SOURCE + ) + } + + private fun IrElement.toConstantValue(): ConstantValue<*> { + return when (this) { + is IrConst<*> -> when (kind) { + IrConstKind.Null -> NullValue() + IrConstKind.Boolean -> BooleanValue(value as Boolean) + IrConstKind.Char -> CharValue(value as Char) + IrConstKind.Byte -> ByteValue(value as Byte) + IrConstKind.Short -> ShortValue(value as Short) + IrConstKind.Int -> IntValue(value as Int) + IrConstKind.Long -> LongValue(value as Long) + IrConstKind.String -> StringValue(value as String) + IrConstKind.Float -> FloatValue(value as Float) + IrConstKind.Double -> DoubleValue(value as Double) + } + + is IrVararg -> { + val elements = elements.map { if (it is IrSpreadElement) error("$it is not expected") else it.toConstantValue() } + ArrayValue(elements) { moduleDescriptor -> + // TODO: substitute. + moduleDescriptor.builtIns.array.defaultType + } + } + + is IrGetEnumValue -> EnumValue(symbol.owner.parentAsClass.toIrBasedDescriptor().classId!!, symbol.owner.name) + + is IrClassReference -> KClassValue((classType.classifierOrFail.owner as IrClass).toIrBasedDescriptor().classId!!, /*TODO*/0) + + is IrConstructorCall -> AnnotationValue(this.toAnnotationDescriptor()) + + else -> error("$this is not expected: ${this.dump()}") + } + } + + override fun getContainingDeclaration() = getContainingDeclaration(owner) + + override fun equals(other: Any?): Boolean { + if (other !is IrBasedDeclarationDescriptor<*>) return false + return owner == other.owner + } + + override fun hashCode(): Int { + return owner.hashCode() + } +} + +fun IrDeclaration.toIrBasedDescriptor(): DeclarationDescriptor = when (this) { + is IrValueParameter -> toIrBasedDescriptor() + is IrTypeParameter -> toIrBasedDescriptor() + is IrVariable -> toIrBasedDescriptor() + is IrLocalDelegatedProperty -> toIrBasedDescriptor() + is IrFunction -> toIrBasedDescriptor() + is IrClass -> toIrBasedDescriptor() + is IrEnumEntry -> toIrBasedDescriptor() + is IrProperty -> toIrBasedDescriptor() + is IrField -> toIrBasedDescriptor() + else -> error("Unknown declaration kind") +} + +abstract class IrBasedCallableDescriptor(owner: T) : CallableDescriptor, IrBasedDeclarationDescriptor(owner) { + + override fun getOriginal() = this + + override fun substitute(substitutor: TypeSubstitutor): CallableDescriptor = + throw UnsupportedOperationException("IrBased descriptors SHOULD NOT be substituted") + + override fun getOverriddenDescriptors(): Collection { + TODO("not implemented") + } + + override fun getSource() = SourceElement.NO_SOURCE + + override fun getExtensionReceiverParameter(): ReceiverParameterDescriptor? = null + + override fun getDispatchReceiverParameter(): ReceiverParameterDescriptor? = null + + override fun getTypeParameters(): List { + TODO("not implemented") + } + + override fun getReturnType(): KotlinType? { + TODO("not implemented") + } + + override fun getValueParameters(): List { + TODO("not implemented") + } + + override fun hasStableParameterNames(): Boolean { + TODO("not implemented") + } + + override fun hasSynthesizedParameterNames() = false + + override fun getVisibility(): Visibility { + TODO("not implemented") + } + + override fun accept(visitor: DeclarationDescriptorVisitor?, data: D): R { + TODO("not implemented") + } + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) { + TODO("not implemented") + } + + override fun getUserData(key: CallableDescriptor.UserDataKey?): V? = null +} + +// Do not create this kind of descriptor for dispatch receiver parameters +// IrBasedReceiverParameterDescriptor should be used instead +open class IrBasedValueParameterDescriptor(owner: IrValueParameter) : ValueParameterDescriptor, + IrBasedCallableDescriptor(owner) { + + override val index get() = owner.index + override val isCrossinline get() = owner.isCrossinline + override val isNoinline get() = owner.isNoinline + override val varargElementType get() = owner.varargElementType?.toIrBasedKotlinType() + override fun isConst() = false + override fun isVar() = false + + override fun getContainingDeclaration() = (owner.parent as IrFunction).toIrBasedDescriptor() + override fun getType() = owner.type.toIrBasedKotlinType() + override fun getName() = owner.name + override fun declaresDefaultValue() = owner.defaultValue != null + override fun getCompileTimeInitializer(): ConstantValue<*>? = null + + override fun copy(newOwner: CallableDescriptor, newName: Name, newIndex: Int) = TODO("not implemented") + + override fun getOverriddenDescriptors(): Collection = emptyList() + override fun getTypeParameters(): List = emptyList() + override fun getValueParameters(): List = emptyList() + + override fun getOriginal() = this + + override fun substitute(substitutor: TypeSubstitutor): ValueParameterDescriptor = + throw UnsupportedOperationException("IrBased descriptors SHOULD NOT be substituted") + + override fun getReturnType(): KotlinType? = owner.type.toIrBasedKotlinType() + + override fun accept(visitor: DeclarationDescriptorVisitor?, data: D) = + visitor!!.visitValueParameterDescriptor(this, data)!! + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) { + visitor!!.visitValueParameterDescriptor(this, null) + } +} + +open class IrBasedReceiverParameterDescriptor(owner: IrValueParameter) : ReceiverParameterDescriptor, + IrBasedCallableDescriptor(owner) { + + override fun getValue(): ReceiverValue { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun getType() = owner.type.toIrBasedKotlinType() + override fun getName() = owner.name + + override fun copy(newOwner: DeclarationDescriptor) = TODO("not implemented") + + override fun getOverriddenDescriptors(): Collection = emptyList() + + override fun getOriginal() = this + + override fun substitute(substitutor: TypeSubstitutor): ReceiverParameterDescriptor { + TODO("") + } + + override fun getReturnType(): KotlinType? = owner.type.toIrBasedKotlinType() + + override fun accept(visitor: DeclarationDescriptorVisitor?, data: D) = + visitor!!.visitReceiverParameterDescriptor(this, data)!! + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) { + visitor!!.visitReceiverParameterDescriptor(this, null) + } +} + +fun IrValueParameter.toIrBasedDescriptor() = + if (index < 0) + IrBasedReceiverParameterDescriptor(this) + else + IrBasedValueParameterDescriptor(this) + +open class IrBasedTypeParameterDescriptor(owner: IrTypeParameter) : TypeParameterDescriptor, + IrBasedCallableDescriptor(owner) { + override fun getName() = owner.name + + override fun isReified() = owner.isReified + + override fun getVariance() = owner.variance + + override fun getUpperBounds() = owner.superTypes.map { it.toIrBasedKotlinType() } + + private val _typeConstructor: TypeConstructor by lazy { + object : AbstractTypeConstructor(LockBasedStorageManager.NO_LOCKS) { + override fun computeSupertypes() = upperBounds + + override val supertypeLoopChecker = SupertypeLoopChecker.EMPTY + + override fun getParameters(): List = emptyList() + + override fun isFinal() = false + + override fun isDenotable() = true + + override fun getDeclarationDescriptor() = this@IrBasedTypeParameterDescriptor + + override fun getBuiltIns() = module.builtIns + } + } + + override fun getTypeConstructor() = _typeConstructor + + override fun getOriginal() = this + + override fun getIndex() = owner.index + + override fun isCapturedFromOuterDeclaration() = false + + private val _defaultType: SimpleType by lazy { + KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( + Annotations.EMPTY, typeConstructor, emptyList(), false, + LazyScopeAdapter { + TypeIntersectionScope.create( + "Scope for type parameter " + name.asString(), + upperBounds + ) + } + ) + } + + override fun getDefaultType() = _defaultType + + override fun getStorageManager() = LockBasedStorageManager.NO_LOCKS + + override fun accept(visitor: DeclarationDescriptorVisitor?, data: D): R = + visitor!!.visitTypeParameterDescriptor(this, data) + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) { + visitor!!.visitTypeParameterDescriptor(this, null) + } + +} + +fun IrTypeParameter.toIrBasedDescriptor() = IrBasedTypeParameterDescriptor(this) + +open class IrBasedVariableDescriptor(owner: IrVariable) : VariableDescriptor, IrBasedCallableDescriptor(owner) { + + override fun getContainingDeclaration() = (owner.parent as IrFunction).toIrBasedDescriptor() + override fun getType() = owner.type.toIrBasedKotlinType() + override fun getReturnType() = getType() + override fun getName() = owner.name + override fun isConst() = owner.isConst + override fun isVar() = owner.isVar + override fun isLateInit() = owner.isLateinit + + override fun getCompileTimeInitializer(): ConstantValue<*>? { + TODO("") + } + + override fun getOverriddenDescriptors(): Collection { + TODO("Not Implemented") + } + + override fun getOriginal() = this + + override fun substitute(substitutor: TypeSubstitutor): VariableDescriptor { + TODO("") + } + + override fun accept(visitor: DeclarationDescriptorVisitor?, data: D): R = + visitor!!.visitVariableDescriptor(this, data) + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) { + visitor!!.visitVariableDescriptor(this, null) + } +} + +fun IrVariable.toIrBasedDescriptor() = IrBasedVariableDescriptor(this) + +open class IrBasedVariableDescriptorWithAccessor(owner: IrLocalDelegatedProperty) : VariableDescriptorWithAccessors, + IrBasedCallableDescriptor(owner) { + override fun getName(): Name = owner.name + + override fun substitute(substitutor: TypeSubstitutor): VariableDescriptor { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun isVar() = owner.isVar + + override fun getCompileTimeInitializer(): ConstantValue<*>? { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun getType(): KotlinType = owner.type.toIrBasedKotlinType() + + override fun isConst(): Boolean = false + + override fun getContainingDeclaration() = (owner.parent as IrFunction).toIrBasedDescriptor() + + override fun isLateInit(): Boolean = false + + override val getter: VariableAccessorDescriptor? + get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates. + override val setter: VariableAccessorDescriptor? + get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates. + override val isDelegated: Boolean = true +} + +fun IrLocalDelegatedProperty.toIrBasedDescriptor() = IrBasedVariableDescriptorWithAccessor(this) + +open class IrBasedSimpleFunctionDescriptor(owner: IrSimpleFunction) : SimpleFunctionDescriptor, + IrBasedCallableDescriptor(owner) { + + override fun getOverriddenDescriptors(): List = owner.overriddenSymbols.map { it.owner.toIrBasedDescriptor() } + + override fun getModality() = owner.modality + override fun getName() = owner.name + override fun getVisibility() = owner.visibility + override fun getReturnType() = owner.returnType.toIrBasedKotlinType() + + override fun getDispatchReceiverParameter() = owner.dispatchReceiverParameter?.run { + (containingDeclaration as ClassDescriptor).thisAsReceiverParameter + } + + override fun getExtensionReceiverParameter() = owner.extensionReceiverParameter?.toIrBasedDescriptor() as? ReceiverParameterDescriptor + override fun getTypeParameters() = owner.typeParameters.map { it.toIrBasedDescriptor() } + override fun getValueParameters() = owner.valueParameters + .asSequence() + .mapNotNull { it.toIrBasedDescriptor() as? ValueParameterDescriptor } + .toMutableList() + + override fun isExternal() = owner.isExternal + override fun isSuspend() = owner.isSuspend + override fun isTailrec() = owner.isTailrec + override fun isInline() = owner.isInline + + override fun isExpect() = false + override fun isActual() = false + override fun isInfix() = false + override fun isOperator() = false + + override fun getOriginal() = this + override fun substitute(substitutor: TypeSubstitutor): SimpleFunctionDescriptor { + TODO("") + } + + override fun setOverriddenDescriptors(overriddenDescriptors: MutableCollection) { + TODO("not implemented") + } + + override fun getKind() = + if (owner.origin == IrDeclarationOrigin.FAKE_OVERRIDE) CallableMemberDescriptor.Kind.FAKE_OVERRIDE + else CallableMemberDescriptor.Kind.SYNTHESIZED + + override fun copy( + newOwner: DeclarationDescriptor?, + modality: Modality?, + visibility: Visibility?, + kind: CallableMemberDescriptor.Kind?, + copyOverrides: Boolean + ): Nothing { + TODO("not implemented") + } + + override fun isHiddenToOvercomeSignatureClash(): Boolean = false + override fun isHiddenForResolutionEverywhereBesideSupercalls(): Boolean = false + + override fun getInitialSignatureDescriptor(): FunctionDescriptor? = null + + override fun getUserData(key: CallableDescriptor.UserDataKey?): V? = null + + override fun newCopyBuilder(): FunctionDescriptor.CopyBuilder { + TODO("not implemented") + } + + override fun accept(visitor: DeclarationDescriptorVisitor?, data: D) = + visitor!!.visitFunctionDescriptor(this, data) + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) { + visitor!!.visitFunctionDescriptor(this, null) + } +} + +class IrBasedFunctionDescriptorWithContainerSource(owner: IrSimpleFunction) : IrBasedSimpleFunctionDescriptor(owner), + DescriptorWithContainerSource { + override val containerSource: DeserializedContainerSource? get() = owner.containerSource +} + +fun IrSimpleFunction.toIrBasedDescriptor() = + if (originalFunction is IrLazyFunction || containerSource != null) + when { + isGetter -> IrBasedPropertyGetterDescriptorWithContainerSource(this) + isSetter -> IrBasedPropertySetterDescriptorWithContainerSource(this) + else -> IrBasedFunctionDescriptorWithContainerSource(this) + } + else when { + isGetter -> IrBasedPropertyGetterDescriptor(this) + isSetter -> IrBasedPropertySetterDescriptor(this) + else -> IrBasedSimpleFunctionDescriptor(this) + } + +open class IrBasedClassConstructorDescriptor(owner: IrConstructor) : ClassConstructorDescriptor, + IrBasedCallableDescriptor(owner) { + override fun getContainingDeclaration() = (owner.parent as IrClass).toIrBasedDescriptor() + + override fun getDispatchReceiverParameter() = owner.dispatchReceiverParameter?.run { + (containingDeclaration.containingDeclaration as ClassDescriptor).thisAsReceiverParameter + } + + override fun getTypeParameters() = + (owner.constructedClass.typeParameters + owner.typeParameters).map { it.toIrBasedDescriptor() } + + override fun getValueParameters() = owner.valueParameters.asSequence() + .mapNotNull { it.toIrBasedDescriptor() as? ValueParameterDescriptor } + .toMutableList() + + override fun getOriginal() = this + + override fun substitute(substitutor: TypeSubstitutor): ClassConstructorDescriptor { + TODO("not implemented") + } + + override fun copy( + newOwner: DeclarationDescriptor, + modality: Modality, + visibility: Visibility, + kind: CallableMemberDescriptor.Kind, + copyOverrides: Boolean + ): ClassConstructorDescriptor { + throw UnsupportedOperationException() + } + + override fun getModality() = Modality.FINAL + + + override fun setOverriddenDescriptors(overriddenDescriptors: MutableCollection) { + TODO("not implemented") + } + + override fun getKind() = CallableMemberDescriptor.Kind.SYNTHESIZED + + override fun getConstructedClass() = (owner.parent as IrClass).toIrBasedDescriptor() + + override fun getName() = owner.name + + override fun getOverriddenDescriptors(): MutableCollection = mutableListOf() + + override fun getInitialSignatureDescriptor(): FunctionDescriptor? = null + + override fun getVisibility() = owner.visibility + + override fun isHiddenToOvercomeSignatureClash(): Boolean { + TODO("not implemented") + } + + override fun isOperator() = false + + override fun isInline() = owner.isInline + + override fun isHiddenForResolutionEverywhereBesideSupercalls(): Boolean { + TODO("not implemented") + } + + override fun getReturnType() = owner.returnType.toIrBasedKotlinType() + + override fun isPrimary() = owner.isPrimary + + override fun isExpect() = false + + override fun isTailrec() = false + + override fun isActual() = false + + override fun isInfix() = false + + override fun isSuspend() = false + + override fun getUserData(key: CallableDescriptor.UserDataKey?): V? = null + + override fun isExternal() = owner.isExternal + + override fun newCopyBuilder(): FunctionDescriptor.CopyBuilder { + TODO("not implemented") + } + + override fun accept(visitor: DeclarationDescriptorVisitor?, data: D): R = + visitor!!.visitConstructorDescriptor(this, data) + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) { + visitor!!.visitConstructorDescriptor(this, null) + } +} + +fun IrConstructor.toIrBasedDescriptor() = IrBasedClassConstructorDescriptor(this) + +fun IrFunction.toIrBasedDescriptor(): FunctionDescriptor = when(this) { + is IrSimpleFunction -> toIrBasedDescriptor() + is IrConstructor -> toIrBasedDescriptor() + else -> error("Unknown function kind") +} + +open class IrBasedClassDescriptor(owner: IrClass) : ClassDescriptor, IrBasedDeclarationDescriptor(owner) { + override fun getName() = owner.name + + override fun getMemberScope(typeArguments: MutableList) = MemberScope.Empty + + override fun getMemberScope(typeSubstitution: TypeSubstitution) = MemberScope.Empty + + override fun getUnsubstitutedMemberScope() = MemberScope.Empty + + override fun getUnsubstitutedInnerClassesScope() = MemberScope.Empty + + override fun getStaticScope() = MemberScope.Empty + + override fun getSource() = SourceElement.NO_SOURCE + + override fun getConstructors() = + owner.declarations.filterIsInstance().filter { !it.origin.isSynthetic }.map { it.toIrBasedDescriptor() }.toList() + + private val _defaultType: SimpleType by lazy { + TypeUtils.makeUnsubstitutedType(this, unsubstitutedMemberScope, KotlinTypeFactory.EMPTY_REFINED_TYPE_FACTORY) + } + + override fun getDefaultType(): SimpleType = _defaultType + + override fun getKind() = owner.kind + + override fun getModality() = owner.modality + + override fun getCompanionObjectDescriptor() = owner.declarations.filterIsInstance().firstOrNull { it.isCompanion }?.toIrBasedDescriptor() + + override fun getVisibility() = owner.visibility + + override fun isCompanionObject() = owner.isCompanion + + override fun isData() = owner.isData + + override fun isInline() = owner.isInline + + override fun isFun() = owner.isFun + + override fun getThisAsReceiverParameter() = owner.thisReceiver?.toIrBasedDescriptor() as ReceiverParameterDescriptor + + override fun getUnsubstitutedPrimaryConstructor() = + owner.declarations.filterIsInstance().singleOrNull { it.isPrimary }?.toIrBasedDescriptor() + + override fun getDeclaredTypeParameters() = owner.typeParameters.map { it.toIrBasedDescriptor() } + + override fun getSealedSubclasses(): Collection { + TODO("not implemented") + } + + override fun getOriginal() = this + + override fun isExpect() = false + + override fun substitute(substitutor: TypeSubstitutor): ClassifierDescriptorWithTypeParameters = + throw UnsupportedOperationException("IrBased descriptors SHOULD NOT be substituted") + + override fun isActual() = false + + private val _typeConstructor: TypeConstructor by lazy { + LazyTypeConstructor( + this, + ::collectTypeParameters, + { owner.superTypes.map { it.toIrBasedKotlinType() } }, + LockBasedStorageManager.NO_LOCKS + ) + } + + private fun collectTypeParameters(): List = + generateSequence(owner as IrTypeParametersContainer, + { current -> + val parent = current.parent as? IrTypeParametersContainer + if (parent is IrClass && current is IrClass && !current.isInner) null + else parent + }) + .flatMap { it.typeParameters } + .map { it.toIrBasedDescriptor() } + .toList() + + override fun getTypeConstructor(): TypeConstructor = _typeConstructor + + override fun isInner() = owner.isInner + + override fun isExternal() = owner.isExternal + + override fun accept(visitor: DeclarationDescriptorVisitor?, data: D): R = + visitor!!.visitClassDescriptor(this, data) + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) { + visitor!!.visitClassDescriptor(this, null) + } + + override fun getDefaultFunctionTypeForSamInterface(): SimpleType? { + TODO("not implemented") + } + + override fun isDefinitelyNotSamInterface(): Boolean { + TODO("not implemented") + } +} + +fun IrClass.toIrBasedDescriptor() = IrBasedClassDescriptor(this) + +open class IrBasedEnumEntryDescriptor(owner: IrEnumEntry) : ClassDescriptor, IrBasedDeclarationDescriptor(owner) { + override fun getName() = owner.name + + override fun getMemberScope(typeArguments: MutableList) = MemberScope.Empty + + override fun getMemberScope(typeSubstitution: TypeSubstitution) = MemberScope.Empty + + override fun getUnsubstitutedMemberScope() = MemberScope.Empty + + override fun getUnsubstitutedInnerClassesScope() = MemberScope.Empty + + override fun getStaticScope() = MemberScope.Empty + + override fun getSource() = SourceElement.NO_SOURCE + + override fun getConstructors() = + getCorrespondingClass().declarations.asSequence().filterIsInstance().map { it.toIrBasedDescriptor() }.toList() + + private fun getCorrespondingClass() = owner.correspondingClass ?: (owner.parent as IrClass) + + private val _defaultType: SimpleType by lazy { + TypeUtils.makeUnsubstitutedType(this, unsubstitutedMemberScope, KotlinTypeFactory.EMPTY_REFINED_TYPE_FACTORY) + } + + override fun getDefaultType(): SimpleType = _defaultType + + override fun getKind() = ClassKind.ENUM_ENTRY + + override fun getModality() = Modality.FINAL + + override fun getCompanionObjectDescriptor() = null + + override fun getVisibility() = Visibilities.DEFAULT_VISIBILITY + + override fun isCompanionObject() = false + + override fun isData() = false + + override fun isInline() = false + + override fun isFun() = false + + override fun getThisAsReceiverParameter() = (owner.parent as IrClass).toIrBasedDescriptor().thisAsReceiverParameter + + override fun getUnsubstitutedPrimaryConstructor(): ClassConstructorDescriptor? { + TODO("not implemented") + } + + override fun getDeclaredTypeParameters(): List = emptyList() + + override fun getSealedSubclasses(): Collection { + TODO("not implemented") + } + + override fun getOriginal() = this + + override fun isExpect() = false + + override fun substitute(substitutor: TypeSubstitutor): ClassifierDescriptorWithTypeParameters = + throw UnsupportedOperationException("IrBased descriptors SHOULD NOT be substituted") + + override fun isActual() = false + + private val _typeConstructor: TypeConstructor by lazy { + ClassTypeConstructorImpl( + this, + emptyList(), + getCorrespondingClass().superTypes.map { it.toIrBasedKotlinType() }, + LockBasedStorageManager.NO_LOCKS + ) + } + + override fun getTypeConstructor(): TypeConstructor = _typeConstructor + + override fun isInner() = false + + override fun isExternal() = false + + override fun accept(visitor: DeclarationDescriptorVisitor?, data: D): R = + visitor!!.visitClassDescriptor(this, data) + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) { + visitor!!.visitClassDescriptor(this, null) + } + + override fun getDefaultFunctionTypeForSamInterface(): SimpleType? = null + + override fun isDefinitelyNotSamInterface() = true +} + +fun IrEnumEntry.toIrBasedDescriptor() = IrBasedEnumEntryDescriptor(this) + +open class IrBasedPropertyDescriptor(owner: IrProperty) : PropertyDescriptor, IrBasedDeclarationDescriptor(owner) { + override fun getModality() = owner.modality + + override fun setOverriddenDescriptors(overriddenDescriptors: MutableCollection) { + TODO("not implemented") + } + + override fun getKind() = CallableMemberDescriptor.Kind.SYNTHESIZED + + override fun getName() = owner.name + + override fun getSource() = SourceElement.NO_SOURCE + + override fun hasSynthesizedParameterNames(): Boolean { + TODO("not implemented") + } + + override fun getOverriddenDescriptors(): MutableCollection = mutableListOf() + + override fun copy( + newOwner: DeclarationDescriptor?, + modality: Modality?, + visibility: Visibility?, + kind: CallableMemberDescriptor.Kind?, + copyOverrides: Boolean + ): CallableMemberDescriptor { + TODO("not implemented") + } + + override fun getValueParameters(): MutableList = mutableListOf() + + override fun getCompileTimeInitializer(): ConstantValue<*>? { + return null + } + + override fun isSetterProjectedOut(): Boolean { + TODO("not implemented") + } + + override fun getAccessors(): List = listOfNotNull(getter, setter) + + override fun getTypeParameters(): List = getter?.typeParameters.orEmpty() + + override fun getVisibility() = owner.visibility + + override val setter: PropertySetterDescriptor? get() = owner.setter?.toIrBasedDescriptor() as? PropertySetterDescriptor + + override fun getOriginal() = this + + override fun isExpect() = false + + override fun substitute(substitutor: TypeSubstitutor): PropertyDescriptor = + throw UnsupportedOperationException("IrBased descriptors SHOULD NOT be substituted") + + override fun isActual() = false + + override fun getReturnType() = (owner.getter?.returnType ?: owner.backingField?.type!!).toIrBasedKotlinType() + + override fun hasStableParameterNames() = false + + override fun getType(): KotlinType = returnType + + override fun isVar() = owner.isVar + + override fun getDispatchReceiverParameter() = owner.getter?.dispatchReceiverParameter?.toIrBasedDescriptor() as? ReceiverParameterDescriptor + + override fun isConst() = owner.isConst + + override fun isLateInit() = owner.isLateinit + + override fun getExtensionReceiverParameter() = owner.getter?.extensionReceiverParameter?.toIrBasedDescriptor() as? ReceiverParameterDescriptor + + override fun isExternal() = owner.isExternal + + override fun accept(visitor: DeclarationDescriptorVisitor?, data: D) = + visitor!!.visitPropertyDescriptor(this, data) + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) { + visitor!!.visitPropertyDescriptor(this, null) + } + + override val getter: PropertyGetterDescriptor? get() = owner.getter?.toIrBasedDescriptor() as? PropertyGetterDescriptor + + override fun newCopyBuilder(): CallableMemberDescriptor.CopyBuilder { + TODO("not implemented") + } + + override val isDelegated get() = owner.isDelegated + + override fun getBackingField(): FieldDescriptor? { + TODO("not implemented") + } + + override fun getDelegateField(): FieldDescriptor? { + TODO("not implemented") + } + + override fun getUserData(key: CallableDescriptor.UserDataKey?): V? = null +} + +class IrBasedPropertyDescriptorWithContainerSource( + owner: IrProperty +) : IrBasedPropertyDescriptor(owner), DescriptorWithContainerSource { + override val containerSource: DeserializedContainerSource? = owner.containerSource +} + +fun IrProperty.toIrBasedDescriptor() = if (originalProperty is IrLazyProperty || containerSource != null) + IrBasedPropertyDescriptorWithContainerSource(this) +else + IrBasedPropertyDescriptor(this) + +abstract class IrBasedPropertyAccessorDescriptor(owner: IrSimpleFunction) : IrBasedSimpleFunctionDescriptor(owner), + PropertyAccessorDescriptor { + override fun isDefault(): Boolean = false + + override fun getOriginal(): IrBasedPropertyAccessorDescriptor = this + + override fun getOverriddenDescriptors() = super.getOverriddenDescriptors().map { it as PropertyAccessorDescriptor } + + override fun getCorrespondingProperty(): PropertyDescriptor = owner.correspondingPropertySymbol!!.owner.toIrBasedDescriptor() + + override val correspondingVariable: VariableDescriptorWithAccessors get() = correspondingProperty +} + +open class IrBasedPropertyGetterDescriptor(owner: IrSimpleFunction) : IrBasedPropertyAccessorDescriptor(owner), PropertyGetterDescriptor { + override fun getOverriddenDescriptors() = super.getOverriddenDescriptors().map { it as PropertyGetterDescriptor } + + override fun getOriginal(): IrBasedPropertyGetterDescriptor = this +} + +class IrBasedPropertyGetterDescriptorWithContainerSource(owner: IrSimpleFunction) : IrBasedPropertyGetterDescriptor(owner), + DescriptorWithContainerSource { + override val containerSource: DeserializedContainerSource? get() = owner.containerSource +} + +open class IrBasedPropertySetterDescriptor(owner: IrSimpleFunction) : IrBasedPropertyAccessorDescriptor(owner), PropertySetterDescriptor { + override fun getOverriddenDescriptors() = super.getOverriddenDescriptors().map { it as PropertySetterDescriptor } + + override fun getOriginal(): IrBasedPropertySetterDescriptor = this +} + +class IrBasedPropertySetterDescriptorWithContainerSource(owner: IrSimpleFunction) : IrBasedPropertySetterDescriptor(owner), + DescriptorWithContainerSource { + override val containerSource: DeserializedContainerSource? get() = owner.containerSource +} + +open class IrBasedTypeAliasDescriptor(owner: IrTypeAlias) : IrBasedDeclarationDescriptor(owner), TypeAliasDescriptor { + + override val underlyingType: SimpleType + get() = throw UnsupportedOperationException("Unexpected use of IrBasedTypeAliasDescriptor $this") + + override val constructors: Collection + get() = throw UnsupportedOperationException("Unexpected use of IrBasedTypeAliasDescriptor $this") + + override fun substitute(substitutor: TypeSubstitutor): ClassifierDescriptorWithTypeParameters = + throw UnsupportedOperationException("IrBased descriptors should not be substituted") + + override fun getDefaultType(): SimpleType = + throw UnsupportedOperationException("Unexpected use of IrBasedTypeAliasDescriptor $this") + + override fun getTypeConstructor(): TypeConstructor = + throw UnsupportedOperationException("Unexpected use of IrBasedTypeAliasDescriptor $this") + + override val expandedType: SimpleType + get() = owner.expandedType.toIrBasedKotlinType() as SimpleType + + override val classDescriptor: ClassDescriptor? + get() = TODO("catch type alias class descriptor") //expandedType.constructor.declarationDescriptor as ClassDescriptor? + + override fun getOriginal(): TypeAliasDescriptor = this + + override fun isInner(): Boolean = false + + override fun getDeclaredTypeParameters(): List = owner.typeParameters.map { it.toIrBasedDescriptor() } + + override fun getName(): Name = owner.name + + override fun getModality(): Modality = Modality.FINAL + + override fun getSource(): SourceElement = SourceElement.NO_SOURCE + + override fun getVisibility(): Visibility = owner.visibility + + override fun isExpect(): Boolean = false + + override fun isActual(): Boolean = owner.isActual + + override fun isExternal(): Boolean = false + + override fun accept(visitor: DeclarationDescriptorVisitor, data: D): R = + visitor.visitTypeAliasDescriptor(this, data) + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor) { + visitor.visitTypeAliasDescriptor(this, null) + } +} + +open class IrBasedFieldDescriptor(owner: IrField) : PropertyDescriptor, IrBasedDeclarationDescriptor(owner) { + override fun getModality() = if (owner.isFinal) Modality.FINAL else Modality.OPEN + + override fun setOverriddenDescriptors(overriddenDescriptors: MutableCollection) { + TODO("not implemented") + } + + override fun getKind() = CallableMemberDescriptor.Kind.SYNTHESIZED + + override fun getName() = owner.name + + override fun getSource() = SourceElement.NO_SOURCE + + override fun hasSynthesizedParameterNames() = false + + override fun getOverriddenDescriptors(): MutableCollection = mutableListOf() + + override fun copy( + newOwner: DeclarationDescriptor?, + modality: Modality?, + visibility: Visibility?, + kind: CallableMemberDescriptor.Kind?, + copyOverrides: Boolean + ): CallableMemberDescriptor { + TODO("not implemented") + } + + override fun getValueParameters(): MutableList = mutableListOf() + + override fun getCompileTimeInitializer(): ConstantValue<*>? { + TODO("not implemented") + } + + override fun isSetterProjectedOut(): Boolean { + TODO("not implemented") + } + + override fun getAccessors(): MutableList = mutableListOf() + + override fun getTypeParameters(): List = emptyList() + + override fun getVisibility() = owner.visibility + + override val setter: PropertySetterDescriptor? get() = null + + override fun getOriginal() = this + + override fun isExpect() = false + + override fun substitute(substitutor: TypeSubstitutor): PropertyDescriptor = + throw UnsupportedOperationException("IrBased descriptors SHOULD NOT be substituted") + + override fun isActual() = false + + override fun getReturnType() = owner.type.toIrBasedKotlinType() + + override fun hasStableParameterNames(): Boolean { + TODO("not implemented") + } + + override fun getType(): KotlinType = owner.type.toIrBasedKotlinType() + + override fun isVar() = !owner.isFinal + + override fun getDispatchReceiverParameter(): ReceiverParameterDescriptor? = + if (owner.isStatic) null else (owner.parentAsClass.thisReceiver?.toIrBasedDescriptor() as ReceiverParameterDescriptor) + + override fun isConst() = false + + override fun isLateInit() = owner.correspondingPropertySymbol?.owner?.isLateinit ?: false + + override fun getExtensionReceiverParameter(): ReceiverParameterDescriptor? = + owner.correspondingPropertySymbol?.owner?.toIrBasedDescriptor()?.extensionReceiverParameter + + override fun isExternal() = owner.isExternal + + override fun accept(visitor: DeclarationDescriptorVisitor?, data: D) = + visitor!!.visitPropertyDescriptor(this, data) + + override fun acceptVoid(visitor: DeclarationDescriptorVisitor?) { + visitor!!.visitPropertyDescriptor(this, null) + } + + override val getter: PropertyGetterDescriptor? get() = null + + override fun newCopyBuilder(): CallableMemberDescriptor.CopyBuilder { + TODO("not implemented") + } + + override val isDelegated get() = false + + // Following functions are used in error reporting when rendering annotations on properties + override fun getBackingField(): FieldDescriptor? = null // TODO + override fun getDelegateField(): FieldDescriptor? = null // TODO + + override fun getUserData(key: CallableDescriptor.UserDataKey?): V? = null +} + +fun IrField.toIrBasedDescriptor() = IrBasedFieldDescriptor(this) + +@OptIn(ObsoleteDescriptorBasedAPI::class) +private fun getContainingDeclaration(declaration: IrDeclaration): DeclarationDescriptor { + val parent = declaration.parent + val parentDescriptor = (parent as IrSymbolOwner).let { + if (it is IrDeclaration) it.toIrBasedDescriptor() else it.symbol.descriptor + } + return if (parent is IrClass && parent.isFileClass) { + // JVM IR adds facade classes for IR of functions/properties loaded both from sources and dependencies. However, these shouldn't + // exist in the descriptor hierarchy, since this is what the old backend (dealing with descriptors) expects. + parentDescriptor.containingDeclaration!! + } else { + parentDescriptor + } +} + +fun IrType.toIrBasedKotlinType(): KotlinType = when (this) { + is IrSimpleType -> makeKotlinType(classifier, arguments, hasQuestionMark) + else -> TODO(toString()) +} + +private fun makeKotlinType( + classifier: IrClassifierSymbol, + arguments: List, + hasQuestionMark: Boolean +): SimpleType = when (classifier) { + is IrTypeParameterSymbol -> classifier.owner.toIrBasedDescriptor().defaultType + is IrClassSymbol -> { + val classDescriptor = classifier.owner.toIrBasedDescriptor() + val kotlinTypeArguments = arguments.mapIndexed { index, it -> + when (it) { + is IrTypeProjection -> TypeProjectionImpl(it.variance, it.type.toIrBasedKotlinType()) + is IrStarProjection -> StarProjectionImpl(classDescriptor.typeConstructor.parameters[index]) + else -> error(it) + } + } + classDescriptor.defaultType.replace(newArguments = kotlinTypeArguments).makeNullableAsSpecified(hasQuestionMark) + } + else -> error("unknown classifier kind $classifier") +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/WrappedDescriptors.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/WrappedDescriptors.kt index eef51375105..734ba5c0211 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/WrappedDescriptors.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/WrappedDescriptors.kt @@ -383,13 +383,6 @@ open class WrappedSimpleFunctionDescriptor( sourceElement: SourceElement = SourceElement.NO_SOURCE ) : SimpleFunctionDescriptor, WrappedCallableDescriptor(annotations, sourceElement) { - // TODO: Remove as soon as all IR declarations have their originalDescriptor. - constructor(originalDescriptor: FunctionDescriptor) : this(originalDescriptor.annotations, originalDescriptor.source) { - this.originalDescriptor = originalDescriptor - } - - var originalDescriptor: FunctionDescriptor? = null - override fun getOverriddenDescriptors() = owner.overriddenSymbols.map { it.descriptor } override fun getContainingDeclaration(): DeclarationDescriptor = getContainingDeclaration(owner) @@ -632,12 +625,23 @@ open class WrappedClassDescriptor( private val _typeConstructor: TypeConstructor by lazy { LazyTypeConstructor( this, - { declaredTypeParameters }, + ::collectTypeParameters, { owner.superTypes.map { it.toKotlinType() } }, LockBasedStorageManager.NO_LOCKS ) } + private fun collectTypeParameters(): List = + generateSequence(owner as IrTypeParametersContainer, + { current -> + val parent = current.parent as? IrTypeParametersContainer + if (parent is IrClass && current is IrClass && !current.isInner) null + else parent + }) + .flatMap { it.typeParameters } + .map { it.descriptor } + .toList() + override fun getTypeConstructor(): TypeConstructor = _typeConstructor override fun isInner() = owner.isInner