From 11e0f427ac61afc78abd420a073a49567a4fed44 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Thu, 13 Feb 2020 18:51:48 +0700 Subject: [PATCH] [Commonizer] Refactor CIR to avoid strong refs on descriptors This is necessary to reduce memory consumption in commonizer while processing sets of massive libraries. Ex: ios_x64 (127 libraries) vs ios_arm64 (127 libraries). --- .../commonizer/builder/builderUtils.kt | 4 +- .../commonizer/builder/propertyDescriptors.kt | 15 +++-- .../commonizer/mergedtree/ir/CirAnnotation.kt | 36 +++++++++-- .../commonizer/mergedtree/ir/CirClass.kt | 59 +++++++++---------- .../commonizer/mergedtree/ir/CirFunction.kt | 40 ++++++------- .../mergedtree/ir/CirFunctionOrProperty.kt | 35 ++++++----- .../commonizer/mergedtree/ir/CirModule.kt | 2 +- .../commonizer/mergedtree/ir/CirProperty.kt | 56 ++++++++++-------- .../commonizer/mergedtree/ir/CirType.kt | 49 ++++++++------- .../commonizer/mergedtree/ir/CirTypeAlias.kt | 15 +++-- .../mergedtree/ir/CirTypeParameter.kt | 18 +++--- .../commonizer/mergedtree/ir/nodeBuilders.kt | 10 ++-- .../commonized/common/package_root.kt | 6 +- .../annotations/commonized/js/package_root.kt | 18 +++--- .../commonized/jvm/package_root.kt | 20 +++---- .../annotations/original/js/package_root.kt | 20 +++---- .../annotations/original/jvm/package_root.kt | 20 +++---- .../DefaultValueParameterCommonizerTest.kt | 6 +- 18 files changed, 236 insertions(+), 193 deletions(-) diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/builder/builderUtils.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/builder/builderUtils.kt index 06036dccb93..a908dc2b07f 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/builder/builderUtils.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/builder/builderUtils.kt @@ -39,7 +39,7 @@ internal fun List.buildDescriptorsAndTypeParameterResolver( targetComponents = targetComponents, typeParameterResolver = typeParameterResolver, containingDeclaration = containingDeclaration, - annotations = param.annotations, + annotations = param.annotations.buildDescriptors(targetComponents), name = param.name, variance = param.variance, isReified = param.isReified, @@ -61,7 +61,7 @@ internal fun List.buildDescriptors( containingDeclaration, null, index, - param.annotations, + param.annotations.buildDescriptors(targetComponents), param.name, param.returnType.buildType(targetComponents, typeParameterResolver), param.declaresDefaultValue, diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/builder/propertyDescriptors.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/builder/propertyDescriptors.kt index 3cbd6fab51e..f6aa370ce27 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/builder/propertyDescriptors.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/builder/propertyDescriptors.kt @@ -80,7 +80,7 @@ private fun CirProperty.buildDescriptor( val getterDescriptor = getter?.let { getter -> DescriptorFactory.createGetter( propertyDescriptor, - getter.annotations, + getter.annotations.buildDescriptors(targetComponents), getter.isDefault, getter.isExternal, getter.isInline @@ -92,8 +92,8 @@ private fun CirProperty.buildDescriptor( val setterDescriptor = setter?.let { setter -> DescriptorFactory.createSetter( propertyDescriptor, - setter.annotations, - setter.parameterAnnotations, + setter.annotations.buildDescriptors(targetComponents), + setter.parameterAnnotations.buildDescriptors(targetComponents), setter.isDefault, setter.isExternal, setter.isInline, @@ -102,8 +102,13 @@ private fun CirProperty.buildDescriptor( ) } - val backingField = backingFieldAnnotations?.let { FieldDescriptorImpl(it, propertyDescriptor) } - val delegateField = delegateFieldAnnotations?.let { FieldDescriptorImpl(it, propertyDescriptor) } + val backingField = backingFieldAnnotations?.let { annotations -> + FieldDescriptorImpl(annotations.buildDescriptors(targetComponents), propertyDescriptor) + } + + val delegateField = delegateFieldAnnotations?.let { annotations -> + FieldDescriptorImpl(annotations.buildDescriptors(targetComponents), propertyDescriptor) + } propertyDescriptor.initialize( getterDescriptor, diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirAnnotation.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirAnnotation.kt index 3f60981e11a..80bb9079a08 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirAnnotation.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirAnnotation.kt @@ -8,9 +8,37 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.constants.ConstantValue +import org.jetbrains.kotlin.resolve.constants.* -class CirAnnotation(private val wrapped: AnnotationDescriptor) { - val fqName: FqName get() = wrapped.fqName ?: error("Annotation with no FQ name: ${wrapped::class.java}, $wrapped") - val allValueArguments: Map> get() = wrapped.allValueArguments +class CirAnnotation(original: AnnotationDescriptor) { + val fqName: FqName = original.fqName ?: error("Annotation with no FQ name: ${original::class.java}, $original") + val allValueArguments: Map> = original.allValueArguments + + init { + allValueArguments.forEach { (name, constantValue) -> + checkSupportedInCommonization(constantValue) { "${original::class.java}, $original[$name]" } + } + } +} + +internal fun checkSupportedInCommonization(constantValue: ConstantValue<*>, location: () -> String) { + @Suppress("TrailingComma") + return when (constantValue) { + is StringValue, + is IntegerValueConstant<*>, + is UnsignedValueConstant<*>, + is BooleanValue, + is NullValue, + is DoubleValue, + is FloatValue, + is EnumValue -> { + // OK + } + is ArrayValue -> { + constantValue.value.forEachIndexed { index, innerConstantValue -> + checkSupportedInCommonization(innerConstantValue) { "${location()}[$index]" } + } + } + else -> error("Unsupported const value type: ${constantValue::class.java}, $constantValue at ${location()}") + } } diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirClass.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirClass.kt index ff0daff53e4..5dde6ee026b 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirClass.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirClass.kt @@ -9,7 +9,6 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe -import kotlin.LazyThreadSafetyMode.PUBLICATION interface CirClass : CirAnnotatedDeclaration, CirNamedDeclaration, CirDeclarationWithTypeParameters, CirDeclarationWithVisibility, CirDeclarationWithModality { val companion: FqName? // null means no companion object @@ -62,39 +61,37 @@ data class CirCommonClassConstructor( override val containingClassIsData get() = unsupported() } -class CirWrappedClass(private val wrapped: ClassDescriptor) : CirClass { - override val annotations by lazy(PUBLICATION) { wrapped.annotations.map(::CirAnnotation) } - override val name get() = wrapped.name - override val typeParameters by lazy(PUBLICATION) { wrapped.declaredTypeParameters.map(::CirWrappedTypeParameter) } - override val companion by lazy(PUBLICATION) { wrapped.companionObjectDescriptor?.fqNameSafe } - override val kind get() = wrapped.kind - override val modality get() = wrapped.modality - override val visibility get() = wrapped.visibility - override val isCompanion get() = wrapped.isCompanionObject - override val isData get() = wrapped.isData - override val isInline get() = wrapped.isInline - override val isInner get() = wrapped.isInner - override val isExternal get() = wrapped.isExternal - override val supertypes by lazy(PUBLICATION) { wrapped.typeConstructor.supertypes.map(CirType.Companion::create) } +class CirClassImpl(original: ClassDescriptor) : CirClass { + override val annotations = original.annotations.map(::CirAnnotation) + override val name = original.name + override val typeParameters = original.declaredTypeParameters.map(::CirTypeParameterImpl) + override val companion = original.companionObjectDescriptor?.fqNameSafe + override val kind = original.kind + override val modality = original.modality + override val visibility = original.visibility + override val isCompanion = original.isCompanionObject + override val isData = original.isData + override val isInline = original.isInline + override val isInner = original.isInner + override val isExternal = original.isExternal + override val supertypes = original.typeConstructor.supertypes.map(CirType.Companion::create) } -class CirWrappedClassConstructor(private val wrapped: ClassConstructorDescriptor) : CirClassConstructor { - override val isPrimary get() = wrapped.isPrimary - override val kind get() = wrapped.kind - override val containingClassKind get() = wrapped.containingDeclaration.kind - override val containingClassModality get() = wrapped.containingDeclaration.modality - override val containingClassIsData get() = wrapped.containingDeclaration.isData - override val annotations by lazy(PUBLICATION) { wrapped.annotations.map(::CirAnnotation) } - override val visibility get() = wrapped.visibility - override val typeParameters by lazy(PUBLICATION) { - wrapped.typeParameters.mapNotNull { typeParameter -> - // save only type parameters that are contributed by the constructor itself - typeParameter.takeIf { it.containingDeclaration == wrapped }?.let(::CirWrappedTypeParameter) - } +class CirClassConstructorImpl(original: ClassConstructorDescriptor) : CirClassConstructor { + override val isPrimary = original.isPrimary + override val kind = original.kind + override val containingClassKind = original.containingDeclaration.kind + override val containingClassModality = original.containingDeclaration.modality + override val containingClassIsData = original.containingDeclaration.isData + override val annotations = original.annotations.map(::CirAnnotation) + override val visibility = original.visibility + override val typeParameters = original.typeParameters.mapNotNull { typeParameter -> + // save only type parameters that are contributed by the constructor itself + typeParameter.takeIf { it.containingDeclaration == original }?.let(::CirTypeParameterImpl) } - override val valueParameters by lazy(PUBLICATION) { wrapped.valueParameters.map(::CirWrappedValueParameter) } - override val hasStableParameterNames get() = wrapped.hasStableParameterNames() - override val hasSynthesizedParameterNames get() = wrapped.hasSynthesizedParameterNames() + override val valueParameters = original.valueParameters.map(::CirValueParameterImpl) + override val hasStableParameterNames = original.hasStableParameterNames() + override val hasSynthesizedParameterNames = original.hasSynthesizedParameterNames() } object CirClassRecursionMarker : CirClass, CirRecursionMarker { diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirFunction.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirFunction.kt index 3560f61b31e..0922a722dfb 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirFunction.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirFunction.kt @@ -6,9 +6,7 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.name.Name -import kotlin.LazyThreadSafetyMode.PUBLICATION interface CirFunctionModifiers { val isOperator: Boolean @@ -41,20 +39,20 @@ data class CirCommonFunction( override val hasSynthesizedParameterNames: Boolean ) : CirCommonFunctionOrProperty(), CirFunction, CirFunctionModifiers by modifiers -class CirWrappedFunction(wrapped: SimpleFunctionDescriptor) : CirWrappedFunctionOrProperty(wrapped), CirFunction { - override val isOperator get() = wrapped.isOperator - override val isInfix get() = wrapped.isInfix - override val isInline get() = wrapped.isInline - override val isTailrec get() = wrapped.isTailrec - override val isSuspend get() = wrapped.isSuspend - override val valueParameters by lazy(PUBLICATION) { wrapped.valueParameters.map(::CirWrappedValueParameter) } - override val hasStableParameterNames get() = wrapped.hasStableParameterNames() - override val hasSynthesizedParameterNames get() = wrapped.hasSynthesizedParameterNames() +class CirFunctionImpl(original: SimpleFunctionDescriptor) : CirFunctionOrPropertyImpl(original), CirFunction { + override val isOperator = original.isOperator + override val isInfix = original.isInfix + override val isInline = original.isInline + override val isTailrec = original.isTailrec + override val isSuspend = original.isSuspend + override val valueParameters = original.valueParameters.map(::CirValueParameterImpl) + override val hasStableParameterNames = original.hasStableParameterNames() + override val hasSynthesizedParameterNames = original.hasSynthesizedParameterNames() } interface CirValueParameter { val name: Name - val annotations: Annotations + val annotations: List val returnType: CirType val varargElementType: CirType? val declaresDefaultValue: Boolean @@ -69,16 +67,16 @@ data class CirCommonValueParameter( override val isCrossinline: Boolean, override val isNoinline: Boolean ) : CirValueParameter { - override val annotations get() = Annotations.EMPTY + override val annotations: List get() = emptyList() override val declaresDefaultValue get() = false } -data class CirWrappedValueParameter(private val wrapped: ValueParameterDescriptor) : CirValueParameter { - override val name get() = wrapped.name - override val annotations get() = wrapped.annotations - override val returnType by lazy(PUBLICATION) { CirType.create(wrapped.returnType!!) } - override val varargElementType by lazy(PUBLICATION) { wrapped.varargElementType?.let(CirType.Companion::create) } - override val declaresDefaultValue get() = wrapped.declaresDefaultValue() - override val isCrossinline get() = wrapped.isCrossinline - override val isNoinline get() = wrapped.isNoinline +class CirValueParameterImpl(original: ValueParameterDescriptor) : CirValueParameter { + override val name = original.name + override val annotations = original.annotations.map(::CirAnnotation) + override val returnType = CirType.create(original.returnType!!) + override val varargElementType = original.varargElementType?.let(CirType.Companion::create) + override val declaresDefaultValue = original.declaresDefaultValue() + override val isCrossinline = original.isCrossinline + override val isNoinline = original.isNoinline } diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirFunctionOrProperty.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirFunctionOrProperty.kt index 771fa793074..0d4a18a3dd2 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirFunctionOrProperty.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirFunctionOrProperty.kt @@ -7,7 +7,6 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir.CirExtensionReceiver.Companion.toReceiver -import kotlin.LazyThreadSafetyMode.PUBLICATION interface CirFunctionOrProperty : CirAnnotatedDeclaration, CirNamedDeclaration, CirDeclarationWithTypeParameters, CirDeclarationWithVisibility, CirDeclarationWithModality, CirMaybeCallableMemberOfClass { val isExternal: Boolean @@ -23,20 +22,26 @@ abstract class CirCommonFunctionOrProperty : CirFunctionOrProperty { final override val containingClassIsData: Boolean? get() = unsupported() } -abstract class CirWrappedFunctionOrProperty(protected val wrapped: T) : CirFunctionOrProperty { - final override val annotations by lazy(PUBLICATION) { wrapped.annotations.map(::CirAnnotation) } - final override val name get() = wrapped.name - final override val modality get() = wrapped.modality - final override val visibility get() = wrapped.visibility - final override val isExternal get() = wrapped.isExternal - final override val extensionReceiver by lazy(PUBLICATION) { wrapped.extensionReceiverParameter?.toReceiver() } - final override val returnType by lazy(PUBLICATION) { CirType.create(wrapped.returnType!!) } - final override val kind get() = wrapped.kind - final override val containingClassKind: ClassKind? get() = containingClass?.kind - final override val containingClassModality: Modality? get() = containingClass?.modality - final override val containingClassIsData: Boolean? get() = containingClass?.isData - final override val typeParameters by lazy(PUBLICATION) { wrapped.typeParameters.map(::CirWrappedTypeParameter) } - private val containingClass: ClassDescriptor? get() = wrapped.containingDeclaration as? ClassDescriptor +abstract class CirFunctionOrPropertyImpl(original: T) : CirFunctionOrProperty { + final override val annotations = original.annotations.map(::CirAnnotation) + final override val name = original.name + final override val modality = original.modality + final override val visibility = original.visibility + final override val isExternal = original.isExternal + final override val extensionReceiver = original.extensionReceiverParameter?.toReceiver() + final override val returnType = CirType.create(original.returnType!!) + final override val kind = original.kind + final override val containingClassKind: ClassKind? + final override val containingClassModality: Modality? + final override val containingClassIsData: Boolean? + final override val typeParameters = original.typeParameters.map(::CirTypeParameterImpl) + + init { + val containingClass = original.containingDeclaration as? ClassDescriptor + containingClassKind = containingClass?.kind + containingClassModality = containingClass?.modality + containingClassIsData = containingClass?.isData + } } data class CirExtensionReceiver( diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirModule.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirModule.kt index 59538888b48..a837b1d784f 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirModule.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirModule.kt @@ -10,5 +10,5 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.name.Name data class CirModule(val name: Name, val builtIns: KotlinBuiltIns) : CirDeclaration { - constructor(descriptor: ModuleDescriptor) : this(descriptor.name, descriptor.builtIns) + constructor(descriptor: ModuleDescriptor) : this(descriptor.name, descriptor.builtIns) // TODO: strong ref } diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirProperty.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirProperty.kt index c693501bd4a..b17c8bb39f2 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirProperty.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirProperty.kt @@ -6,12 +6,10 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir.CirGetter.Companion.toGetter import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir.CirSetter.Companion.toSetter import org.jetbrains.kotlin.resolve.constants.ConstantValue -import kotlin.LazyThreadSafetyMode.PUBLICATION interface CirProperty : CirFunctionOrProperty { val isVar: Boolean @@ -20,8 +18,8 @@ interface CirProperty : CirFunctionOrProperty { val isDelegate: Boolean val getter: CirGetter? val setter: CirSetter? - val backingFieldAnnotations: Annotations? // null assumes no backing field - val delegateFieldAnnotations: Annotations? // null assumes no backing field + val backingFieldAnnotations: List? // null assumes no backing field + val delegateFieldAnnotations: List? // null assumes no backing field val compileTimeInitializer: ConstantValue<*>? } @@ -41,50 +39,56 @@ data class CirCommonProperty( override val isConst get() = false override val isDelegate get() = false override val getter get() = CirGetter.DEFAULT_NO_ANNOTATIONS - override val backingFieldAnnotations: Annotations? get() = null - override val delegateFieldAnnotations: Annotations? get() = null + override val backingFieldAnnotations: List? get() = null + override val delegateFieldAnnotations: List? get() = null override val compileTimeInitializer: ConstantValue<*>? get() = null } -class CirWrappedProperty(wrapped: PropertyDescriptor) : CirWrappedFunctionOrProperty(wrapped), CirProperty { - override val isVar get() = wrapped.isVar - override val isLateInit get() = wrapped.isLateInit - override val isConst get() = wrapped.isConst - override val isDelegate get() = @Suppress("DEPRECATION") wrapped.isDelegated - override val getter by lazy(PUBLICATION) { wrapped.getter?.toGetter() } - override val setter by lazy(PUBLICATION) { wrapped.setter?.toSetter() } - override val backingFieldAnnotations get() = wrapped.backingField?.annotations - override val delegateFieldAnnotations get() = wrapped.delegateField?.annotations - override val compileTimeInitializer get() = wrapped.compileTimeInitializer +class CirPropertyImpl(original: PropertyDescriptor) : CirFunctionOrPropertyImpl(original), CirProperty { + override val isVar = original.isVar + override val isLateInit = original.isLateInit + override val isConst = original.isConst + override val isDelegate = @Suppress("DEPRECATION") original.isDelegated + override val getter = original.getter?.toGetter() + override val setter = original.setter?.toSetter() + override val backingFieldAnnotations = original.backingField?.annotations?.map(::CirAnnotation) + override val delegateFieldAnnotations = original.delegateField?.annotations?.map(::CirAnnotation) + override val compileTimeInitializer = original.compileTimeInitializer + + init { + compileTimeInitializer?.let { compileTimeInitializer -> + checkSupportedInCommonization(compileTimeInitializer) { "${original::class.java}, $original" } + } + } } interface CirPropertyAccessor { - val annotations: Annotations + val annotations: List val isDefault: Boolean val isExternal: Boolean val isInline: Boolean } data class CirGetter( - override val annotations: Annotations, + override val annotations: List, override val isDefault: Boolean, override val isExternal: Boolean, override val isInline: Boolean ) : CirPropertyAccessor { companion object { - val DEFAULT_NO_ANNOTATIONS = CirGetter(Annotations.EMPTY, isDefault = true, isExternal = false, isInline = false) + val DEFAULT_NO_ANNOTATIONS = CirGetter(emptyList(), isDefault = true, isExternal = false, isInline = false) fun PropertyGetterDescriptor.toGetter() = if (isDefault && annotations.isEmpty()) DEFAULT_NO_ANNOTATIONS else - CirGetter(annotations, isDefault, isExternal, isInline) + CirGetter(annotations.map(::CirAnnotation), isDefault, isExternal, isInline) } } data class CirSetter( - override val annotations: Annotations, - val parameterAnnotations: Annotations, + override val annotations: List, + val parameterAnnotations: List, override val visibility: Visibility, override val isDefault: Boolean, override val isExternal: Boolean, @@ -92,8 +96,8 @@ data class CirSetter( ) : CirPropertyAccessor, CirDeclarationWithVisibility { companion object { fun createDefaultNoAnnotations(visibility: Visibility) = CirSetter( - Annotations.EMPTY, - Annotations.EMPTY, + emptyList(), + emptyList(), visibility, isDefault = visibility == Visibilities.PUBLIC, isExternal = false, @@ -101,8 +105,8 @@ data class CirSetter( ) fun PropertySetterDescriptor.toSetter() = CirSetter( - annotations, - valueParameters.single().annotations, + annotations.map(::CirAnnotation), + valueParameters.single().annotations.map(::CirAnnotation), visibility, isDefault, isExternal, diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirType.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirType.kt index 55dd2e3ad38..8963ef9633f 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirType.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirType.kt @@ -16,7 +16,6 @@ import org.jetbrains.kotlin.descriptors.commonizer.utils.fqName import org.jetbrains.kotlin.descriptors.commonizer.utils.fqNameWithTypeParameters import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.types.* -import kotlin.LazyThreadSafetyMode.PUBLICATION sealed class CirType { companion object { @@ -36,25 +35,35 @@ sealed class CirType { * This is necessary to properly compare types for type aliases, where abbreviation type represents the type alias itself while * expanded type represents right-hand side declaration that should be processed separately. * - * There is no difference between [abbreviation] and [expanded] for types representing classes and type parameters. + * There is no difference between "abbreviation" and "expanded" for types representing classes and type parameters. */ -class CirSimpleType(private val wrapped: SimpleType) : CirType() { - val annotations by lazy(PUBLICATION) { abbreviation.annotations.map(::CirAnnotation) } - val kind = CirSimpleTypeKind.determineKind(abbreviation.declarationDescriptor) - val fqName by lazy(PUBLICATION) { abbreviation.fqName } - val arguments by lazy(PUBLICATION) { abbreviation.arguments.map(::CirTypeProjection) } - val isMarkedNullable get() = abbreviation.isMarkedNullable - val isDefinitelyNotNullType get() = abbreviation.isDefinitelyNotNullType - val expandedTypeConstructorId by lazy(PUBLICATION) { CirTypeConstructorId(expanded) } +class CirSimpleType(original: SimpleType) : CirType() { + val annotations: List + val kind: CirSimpleTypeKind + val fqName: FqName + val arguments: List + val isMarkedNullable: Boolean + val isDefinitelyNotNullType: Boolean + val expandedTypeConstructorId: CirTypeConstructorId + + init { + val abbreviation = (original as? AbbreviatedType)?.abbreviation ?: original + val expanded = (original as? AbbreviatedType)?.expandedType ?: original + + annotations = abbreviation.annotations.map(::CirAnnotation) + kind = CirSimpleTypeKind.determineKind(abbreviation.declarationDescriptor) + fqName = abbreviation.fqName + arguments = abbreviation.arguments.map(::CirTypeProjection) + isMarkedNullable = abbreviation.isMarkedNullable + isDefinitelyNotNullType = abbreviation.isDefinitelyNotNullType + expandedTypeConstructorId = CirTypeConstructorId(expanded) + } inline val isClassOrTypeAlias get() = (kind == CLASS || kind == TYPE_ALIAS) - val fqNameWithTypeParameters by lazy(PUBLICATION) { wrapped.fqNameWithTypeParameters } + val fqNameWithTypeParameters = original.fqNameWithTypeParameters - override fun equals(other: Any?) = wrapped == (other as? CirSimpleType)?.wrapped - override fun hashCode() = wrapped.hashCode() - - private inline val abbreviation: SimpleType get() = (wrapped as? AbbreviatedType)?.abbreviation ?: wrapped - private inline val expanded: SimpleType get() = (wrapped as? AbbreviatedType)?.expandedType ?: wrapped + override fun equals(other: Any?) = fqNameWithTypeParameters == (other as? CirSimpleType)?.fqNameWithTypeParameters + override fun hashCode() = fqNameWithTypeParameters.hashCode() } enum class CirSimpleTypeKind { @@ -79,10 +88,10 @@ data class CirTypeConstructorId(val fqName: FqName, val numberOfTypeParameters: constructor(type: SimpleType) : this(type.fqName, type.constructor.parameters.size) } -class CirTypeProjection(private val wrapped: TypeProjection) { - val projectionKind get() = wrapped.projectionKind - val isStarProjection get() = wrapped.isStarProjection - val type by lazy(PUBLICATION) { CirType.create(wrapped.type) } +class CirTypeProjection(original: TypeProjection) { + val projectionKind = original.projectionKind + val isStarProjection = original.isStarProjection + val type = CirType.create(original.type) } data class CirFlexibleType(val lowerBound: CirSimpleType, val upperBound: CirSimpleType) : CirType() diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirTypeAlias.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirTypeAlias.kt index 8af491f1339..9bdb3d465bf 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirTypeAlias.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirTypeAlias.kt @@ -6,18 +6,17 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor -import kotlin.LazyThreadSafetyMode.PUBLICATION interface CirTypeAlias : CirAnnotatedDeclaration, CirNamedDeclaration, CirDeclarationWithTypeParameters, CirDeclarationWithVisibility { val underlyingType: CirSimpleType val expandedType: CirSimpleType } -class CirWrappedTypeAlias(private val wrapped: TypeAliasDescriptor) : CirTypeAlias { - override val annotations by lazy(PUBLICATION) { wrapped.annotations.map(::CirAnnotation) } - override val name get() = wrapped.name - override val typeParameters by lazy(PUBLICATION) { wrapped.declaredTypeParameters.map(::CirWrappedTypeParameter) } - override val visibility get() = wrapped.visibility - override val underlyingType by lazy(PUBLICATION) { CirSimpleType(wrapped.underlyingType) } - override val expandedType by lazy(PUBLICATION) { CirSimpleType(wrapped.expandedType) } +class CirTypeAliasImpl(original: TypeAliasDescriptor) : CirTypeAlias { + override val annotations = original.annotations.map(::CirAnnotation) + override val name = original.name + override val typeParameters = original.declaredTypeParameters.map(::CirTypeParameterImpl) + override val visibility = original.visibility + override val underlyingType = CirSimpleType(original.underlyingType) + override val expandedType = CirSimpleType(original.expandedType) } diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirTypeParameter.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirTypeParameter.kt index dd22072a7df..f6027d9b503 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirTypeParameter.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/CirTypeParameter.kt @@ -6,13 +6,11 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor -import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.types.Variance -import kotlin.LazyThreadSafetyMode.PUBLICATION interface CirTypeParameter { - val annotations: Annotations + val annotations: List val name: Name val isReified: Boolean val variance: Variance @@ -25,13 +23,13 @@ data class CirCommonTypeParameter( override val variance: Variance, override val upperBounds: List ) : CirTypeParameter { - override val annotations get() = Annotations.EMPTY + override val annotations: List get() = emptyList() } -data class CirWrappedTypeParameter(private val wrapped: TypeParameterDescriptor) : CirTypeParameter { - override val annotations get() = wrapped.annotations - override val name get() = wrapped.name - override val isReified get() = wrapped.isReified - override val variance get() = wrapped.variance - override val upperBounds by lazy(PUBLICATION) { wrapped.upperBounds.map(CirType.Companion::create) } +class CirTypeParameterImpl(original: TypeParameterDescriptor) : CirTypeParameter { + override val annotations = original.annotations.map(::CirAnnotation) + override val name = original.name + override val isReified = original.isReified + override val variance = original.variance + override val upperBounds = original.upperBounds.map(CirType.Companion::create) } diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/nodeBuilders.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/nodeBuilders.kt index eec1969d590..365409c7e63 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/nodeBuilders.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/ir/nodeBuilders.kt @@ -66,7 +66,7 @@ internal fun buildPropertyNode( ): CirPropertyNode = buildNode( storageManager = storageManager, descriptors = properties, - targetDeclarationProducer = ::CirWrappedProperty, + targetDeclarationProducer = ::CirPropertyImpl, commonValueProducer = { commonize(containingDeclarationCommon, it, PropertyCommonizer(cache)) }, recursionMarker = null, nodeProducer = ::CirPropertyNode @@ -80,7 +80,7 @@ internal fun buildFunctionNode( ): CirFunctionNode = buildNode( storageManager = storageManager, descriptors = functions, - targetDeclarationProducer = ::CirWrappedFunction, + targetDeclarationProducer = ::CirFunctionImpl, commonValueProducer = { commonize(containingDeclarationCommon, it, FunctionCommonizer(cache)) }, recursionMarker = null, nodeProducer = ::CirFunctionNode @@ -94,7 +94,7 @@ internal fun buildClassNode( ): CirClassNode = buildNode( storageManager = storageManager, descriptors = classes, - targetDeclarationProducer = ::CirWrappedClass, + targetDeclarationProducer = ::CirClassImpl, commonValueProducer = { commonize(containingDeclarationCommon, it, ClassCommonizer(cacheRW)) }, recursionMarker = CirClassRecursionMarker, nodeProducer = ::CirClassNode @@ -113,7 +113,7 @@ internal fun buildClassConstructorNode( ): CirClassConstructorNode = buildNode( storageManager = storageManager, descriptors = constructors, - targetDeclarationProducer = ::CirWrappedClassConstructor, + targetDeclarationProducer = ::CirClassConstructorImpl, commonValueProducer = { commonize(containingDeclarationCommon, it, ClassConstructorCommonizer(cache)) }, recursionMarker = null, nodeProducer = ::CirClassConstructorNode @@ -126,7 +126,7 @@ internal fun buildTypeAliasNode( ): CirTypeAliasNode = buildNode( storageManager = storageManager, descriptors = typeAliases, - targetDeclarationProducer = ::CirWrappedTypeAlias, + targetDeclarationProducer = ::CirTypeAliasImpl, commonValueProducer = { commonize(it, TypeAliasCommonizer(cacheRW)) }, recursionMarker = CirClassRecursionMarker, nodeProducer = ::CirTypeAliasNode diff --git a/native/commonizer/testData/generalCommonization/annotations/commonized/common/package_root.kt b/native/commonizer/testData/generalCommonization/annotations/commonized/common/package_root.kt index 17b4eccd559..88a79904e2a 100644 --- a/native/commonizer/testData/generalCommonization/annotations/commonized/common/package_root.kt +++ b/native/commonizer/testData/generalCommonization/annotations/commonized/common/package_root.kt @@ -1,8 +1,8 @@ expect annotation class CommonAnnotationForAnnotationClassesOnly(text: String) { val text: String } expect annotation class CommonAnnotation(text: String) { val text: String } -expect annotation class CommonOuterAnnotation(inner: CommonInnerAnnotation) { val inner: CommonInnerAnnotation } -expect annotation class CommonInnerAnnotation(text: String) { val text: String } +//expect annotation class CommonOuterAnnotation(inner: CommonInnerAnnotation) { val inner: CommonInnerAnnotation } +//expect annotation class CommonInnerAnnotation(text: String) { val text: String } expect var propertyWithoutBackingField: Double expect val propertyWithBackingField: Double @@ -15,4 +15,4 @@ expect fun Q.function2(): Q expect class AnnotatedClass(value: String) { val value: String } expect class AnnotatedTypeAlias -expect object ObjectWithNestedAnnotations +//expect object ObjectWithNestedAnnotations diff --git a/native/commonizer/testData/generalCommonization/annotations/commonized/js/package_root.kt b/native/commonizer/testData/generalCommonization/annotations/commonized/js/package_root.kt index 0703b048eb5..d06a97a31f2 100644 --- a/native/commonizer/testData/generalCommonization/annotations/commonized/js/package_root.kt +++ b/native/commonizer/testData/generalCommonization/annotations/commonized/js/package_root.kt @@ -16,13 +16,13 @@ annotation class JsAnnotationForAnnotationClassesOnly(val text: String) @CommonAnnotationForAnnotationClassesOnly("annotation-class") annotation class JsAnnotation(val text: String) -@Target(AnnotationTarget.CLASS) -actual annotation class CommonOuterAnnotation(actual val inner: CommonInnerAnnotation) -actual annotation class CommonInnerAnnotation(actual val text: String) +//@Target(AnnotationTarget.CLASS) +//actual annotation class CommonOuterAnnotation(actual val inner: CommonInnerAnnotation) +//actual annotation class CommonInnerAnnotation(actual val text: String) -@Target(AnnotationTarget.CLASS) -annotation class JsOuterAnnotation(val inner: JsInnerAnnotation) -annotation class JsInnerAnnotation(val text: String) +//@Target(AnnotationTarget.CLASS) +//annotation class JsOuterAnnotation(val inner: JsInnerAnnotation) +//annotation class JsInnerAnnotation(val text: String) @JsAnnotation("property") @CommonAnnotation("property") @@ -57,6 +57,6 @@ actual class AnnotatedClass @JsAnnotation("constructor") @CommonAnnotation("cons @CommonAnnotation("type-alias") actual typealias AnnotatedTypeAlias = AnnotatedClass -@JsOuterAnnotation(inner = JsInnerAnnotation("nested-annotations")) -@CommonOuterAnnotation(inner = CommonInnerAnnotation("nested-annotations")) -actual object ObjectWithNestedAnnotations +//@JsOuterAnnotation(inner = JsInnerAnnotation("nested-annotations")) +//@CommonOuterAnnotation(inner = CommonInnerAnnotation("nested-annotations")) +//actual object ObjectWithNestedAnnotations diff --git a/native/commonizer/testData/generalCommonization/annotations/commonized/jvm/package_root.kt b/native/commonizer/testData/generalCommonization/annotations/commonized/jvm/package_root.kt index c031d2bc23c..7b7d2bdc2cf 100644 --- a/native/commonizer/testData/generalCommonization/annotations/commonized/jvm/package_root.kt +++ b/native/commonizer/testData/generalCommonization/annotations/commonized/jvm/package_root.kt @@ -16,13 +16,13 @@ annotation class JvmAnnotationForAnnotationClassesOnly(val text: String) @CommonAnnotationForAnnotationClassesOnly("annotation-class") annotation class JvmAnnotation(val text: String) -@Target(AnnotationTarget.CLASS) -actual annotation class CommonOuterAnnotation(actual val inner: CommonInnerAnnotation) -actual annotation class CommonInnerAnnotation(actual val text: String) - -@Target(AnnotationTarget.CLASS) -annotation class JvmOuterAnnotation(val inner: JvmInnerAnnotation) -annotation class JvmInnerAnnotation(val text: String) +//@Target(AnnotationTarget.CLASS) +//actual annotation class CommonOuterAnnotation(actual val inner: CommonInnerAnnotation) +//actual annotation class CommonInnerAnnotation(actual val text: String) +// +//@Target(AnnotationTarget.CLASS) +//annotation class JvmOuterAnnotation(val inner: JvmInnerAnnotation) +//annotation class JvmInnerAnnotation(val text: String) @JvmAnnotation("property") @CommonAnnotation("property") @@ -57,6 +57,6 @@ actual class AnnotatedClass @JvmAnnotation("constructor") @CommonAnnotation("con @CommonAnnotation("type-alias") actual typealias AnnotatedTypeAlias = AnnotatedClass -@JvmOuterAnnotation(inner = JvmInnerAnnotation("nested-annotations")) -@CommonOuterAnnotation(inner = CommonInnerAnnotation("nested-annotations")) -actual object ObjectWithNestedAnnotations +//@JvmOuterAnnotation(inner = JvmInnerAnnotation("nested-annotations")) +//@CommonOuterAnnotation(inner = CommonInnerAnnotation("nested-annotations")) +//actual object ObjectWithNestedAnnotations diff --git a/native/commonizer/testData/generalCommonization/annotations/original/js/package_root.kt b/native/commonizer/testData/generalCommonization/annotations/original/js/package_root.kt index 9241f5a88f4..f798009e603 100644 --- a/native/commonizer/testData/generalCommonization/annotations/original/js/package_root.kt +++ b/native/commonizer/testData/generalCommonization/annotations/original/js/package_root.kt @@ -16,13 +16,13 @@ annotation class JsAnnotationForAnnotationClassesOnly(val text: String) @CommonAnnotationForAnnotationClassesOnly("annotation-class") annotation class JsAnnotation(val text: String) -@Target(AnnotationTarget.CLASS) -annotation class CommonOuterAnnotation(val inner: CommonInnerAnnotation) -annotation class CommonInnerAnnotation(val text: String) - -@Target(AnnotationTarget.CLASS) -annotation class JsOuterAnnotation(val inner: JsInnerAnnotation) -annotation class JsInnerAnnotation(val text: String) +//@Target(AnnotationTarget.CLASS) +//annotation class CommonOuterAnnotation(val inner: CommonInnerAnnotation) +//annotation class CommonInnerAnnotation(val text: String) +// +//@Target(AnnotationTarget.CLASS) +//annotation class JsOuterAnnotation(val inner: JsInnerAnnotation) +//annotation class JsInnerAnnotation(val text: String) @JsAnnotation("property") @CommonAnnotation("property") @@ -57,6 +57,6 @@ class AnnotatedClass @JsAnnotation("constructor") @CommonAnnotation("constructor @CommonAnnotation("type-alias") typealias AnnotatedTypeAlias = AnnotatedClass -@JsOuterAnnotation(inner = JsInnerAnnotation("nested-annotations")) -@CommonOuterAnnotation(inner = CommonInnerAnnotation("nested-annotations")) -object ObjectWithNestedAnnotations +//@JsOuterAnnotation(inner = JsInnerAnnotation("nested-annotations")) +//@CommonOuterAnnotation(inner = CommonInnerAnnotation("nested-annotations")) +//object ObjectWithNestedAnnotations diff --git a/native/commonizer/testData/generalCommonization/annotations/original/jvm/package_root.kt b/native/commonizer/testData/generalCommonization/annotations/original/jvm/package_root.kt index 759195b8f95..c55eff8e7db 100644 --- a/native/commonizer/testData/generalCommonization/annotations/original/jvm/package_root.kt +++ b/native/commonizer/testData/generalCommonization/annotations/original/jvm/package_root.kt @@ -16,13 +16,13 @@ annotation class JvmAnnotationForAnnotationClassesOnly(val text: String) @CommonAnnotationForAnnotationClassesOnly("annotation-class") annotation class JvmAnnotation(val text: String) -@Target(AnnotationTarget.CLASS) -annotation class CommonOuterAnnotation(val inner: CommonInnerAnnotation) -annotation class CommonInnerAnnotation(val text: String) - -@Target(AnnotationTarget.CLASS) -annotation class JvmOuterAnnotation(val inner: JvmInnerAnnotation) -annotation class JvmInnerAnnotation(val text: String) +//@Target(AnnotationTarget.CLASS) +//annotation class CommonOuterAnnotation(val inner: CommonInnerAnnotation) +//annotation class CommonInnerAnnotation(val text: String) +// +//@Target(AnnotationTarget.CLASS) +//annotation class JvmOuterAnnotation(val inner: JvmInnerAnnotation) +//annotation class JvmInnerAnnotation(val text: String) @JvmAnnotation("property") @CommonAnnotation("property") @@ -57,6 +57,6 @@ class AnnotatedClass @JvmAnnotation("constructor") @CommonAnnotation("constructo @CommonAnnotation("type-alias") typealias AnnotatedTypeAlias = AnnotatedClass -@JvmOuterAnnotation(inner = JvmInnerAnnotation("nested-annotations")) -@CommonOuterAnnotation(inner = CommonInnerAnnotation("nested-annotations")) -object ObjectWithNestedAnnotations +//@JvmOuterAnnotation(inner = JvmInnerAnnotation("nested-annotations")) +//@CommonOuterAnnotation(inner = CommonInnerAnnotation("nested-annotations")) +//object ObjectWithNestedAnnotations diff --git a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/DefaultValueParameterCommonizerTest.kt b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/DefaultValueParameterCommonizerTest.kt index c7358bab143..29707550e84 100644 --- a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/DefaultValueParameterCommonizerTest.kt +++ b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/DefaultValueParameterCommonizerTest.kt @@ -5,9 +5,9 @@ package org.jetbrains.kotlin.descriptors.commonizer.core -import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.commonizer.utils.EMPTY_CLASSIFIERS_CACHE import org.jetbrains.kotlin.descriptors.commonizer.core.CirTestValueParameter.Companion.areEqual +import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir.CirAnnotation import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir.CirClassifiersCache import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir.CirType import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir.CirValueParameter @@ -157,7 +157,7 @@ class DefaultValueParameterCommonizerTest : AbstractCommonizerTest, override val returnType: CirType, override val varargElementType: CirType?, override val declaresDefaultValue: Boolean,