From 1d0fdd79761deac245869e265ae49665d8b19054 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Mon, 18 Feb 2019 23:12:37 +0500 Subject: [PATCH] [IR] Some fixes to wrapped descriptors --- .../common/descriptors/WrappedDescriptors.kt | 112 ++++++++++++++++-- 1 file changed, 99 insertions(+), 13 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/descriptors/WrappedDescriptors.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/descriptors/WrappedDescriptors.kt index ab60545d2da..7bf2fc19363 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/descriptors/WrappedDescriptors.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/descriptors/WrappedDescriptors.kt @@ -6,13 +6,22 @@ package org.jetbrains.kotlin.backend.common.descriptors import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.impl.ReceiverParameterDescriptorImpl +import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl +import org.jetbrains.kotlin.ir.expressions.* +import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl +import org.jetbrains.kotlin.ir.types.classifierOrFail import org.jetbrains.kotlin.ir.types.toKotlinType +import org.jetbrains.kotlin.ir.util.defaultType +import org.jetbrains.kotlin.ir.util.dump import org.jetbrains.kotlin.ir.util.parentAsClass import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.constants.ConstantValue +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 @@ -24,12 +33,66 @@ import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.types.* -abstract class WrappedDeclarationDescriptor(override val annotations: Annotations) : DeclarationDescriptor { - lateinit var owner: T - private var bound = false +abstract class WrappedDeclarationDescriptor(annotations: Annotations) : DeclarationDescriptor { + private val annotations_ = annotations + + override val annotations: Annotations + get() = if (annotations_ != Annotations.EMPTY) annotations_ else annotationsFromOwner + + private val annotationsFromOwner by lazy { + val ownerAnnotations = (owner as? IrAnnotationContainer)?.annotations ?: return@lazy Annotations.EMPTY + Annotations.create(ownerAnnotations.map { call -> + AnnotationDescriptorImpl( + call.symbol.owner.parentAsClass.defaultType.toKotlinType(), + call.symbol.owner.valueParameters.associate { it.name to call.getValueArgument(it.index)!!.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) + } + + this 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 + } + } + + this is IrGetEnumValue -> EnumValue(symbol.owner.parentAsClass.descriptor.classId!!, symbol.owner.name) + + this is IrClassReference -> KClassValue(classType.classifierOrFail.descriptor.classId!!, /*TODO*/0) + + else -> error("$this is not expected") + } + } + + private var _owner: T? = null + + var owner: T + get() { + return _owner ?: error("$this is not bound") + } + private set(value) { + _owner?.let { error("$this is already bound to ${it.dump()}") } + _owner = value + } + fun bind(declaration: T) { - assert(!bound) - bound = true owner = declaration } } @@ -518,7 +581,12 @@ open class WrappedClassDescriptor( override fun isActual() = false private val _typeConstructor: TypeConstructor by lazy { - ClassTypeConstructorImpl(this, emptyList(), owner.superTypes.map { it.toKotlinType() }, LockBasedStorageManager.NO_LOCKS) + LazyTypeConstructor( + this, + { emptyList() }, + { owner.superTypes.map { it.toKotlinType() } }, + LockBasedStorageManager.NO_LOCKS + ) } override fun getTypeConstructor(): TypeConstructor = _typeConstructor @@ -535,6 +603,28 @@ open class WrappedClassDescriptor( } } +class LazyTypeConstructor( + val classDescriptor: ClassDescriptor, + val parametersBuilder: () -> List, + val superTypesBuilder: () -> List, + storageManager: StorageManager +) : AbstractClassTypeConstructor(storageManager) { + val parameters_ by lazy { parametersBuilder() } + val superTypes_ by lazy { superTypesBuilder() } + + override fun getParameters() = parameters_ + + override fun computeSupertypes() = superTypes_ + + override fun isDenotable() = true + + override fun getDeclarationDescriptor() = classDescriptor + + override val supertypeLoopChecker: SupertypeLoopChecker + get() = SupertypeLoopChecker.EMPTY + +} + open class WrappedEnumEntryDescriptor( annotations: Annotations = Annotations.EMPTY, private val sourceElement: SourceElement = SourceElement.NO_SOURCE @@ -698,9 +788,7 @@ open class WrappedPropertyDescriptor( override fun isVar() = owner.isVar - override fun getDispatchReceiverParameter(): ReceiverParameterDescriptor? { - TODO("not implemented") - } + override fun getDispatchReceiverParameter() = owner.getter?.dispatchReceiverParameter?.descriptor as? ReceiverParameterDescriptor override fun isConst() = owner.isConst @@ -708,9 +796,7 @@ open class WrappedPropertyDescriptor( override fun isLateInit() = owner.isLateinit - override fun getExtensionReceiverParameter(): ReceiverParameterDescriptor? { - TODO("not implemented") - } + override fun getExtensionReceiverParameter() = owner.getter?.extensionReceiverParameter?.descriptor as? ReceiverParameterDescriptor override fun isExternal() = owner.isExternal