From f3e28c3767afb9309edfb1e00f9c59bef60e6135 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Thu, 17 Feb 2022 16:07:57 +0300 Subject: [PATCH] FIR: Prepare ConeAttribute infrastructure for non-singleton attrs --- .../fir/types/CompilerConeAttributes.kt | 17 +++++++------- .../kotlin/fir/types/ConeAttributes.kt | 10 +++++++-- .../fir/serialization/FirElementSerializer.kt | 22 ++++++++----------- .../kotlin/fir/types/ConeInferenceContext.kt | 4 ++-- .../kotlin/fir/types/ConeTypeContext.kt | 10 ++++----- 5 files changed, 33 insertions(+), 30 deletions(-) diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/CompilerConeAttributes.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/CompilerConeAttributes.kt index d8bda6c5a4d..0d429840f38 100644 --- a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/CompilerConeAttributes.kt +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/CompilerConeAttributes.kt @@ -81,20 +81,21 @@ object CompilerConeAttributes { override fun toString(): String = "@UnsafeVariance" } - val compilerAttributeByClassId: Map> = mapOf( - Exact.ANNOTATION_CLASS_ID to Exact, - NoInfer.ANNOTATION_CLASS_ID to NoInfer, - EnhancedNullability.ANNOTATION_CLASS_ID to EnhancedNullability, - ExtensionFunctionType.ANNOTATION_CLASS_ID to ExtensionFunctionType, - UnsafeVariance.ANNOTATION_CLASS_ID to UnsafeVariance + private val compilerAttributeByClassId: Map = mapOf( + Exact.ANNOTATION_CLASS_ID to Exact.key, + NoInfer.ANNOTATION_CLASS_ID to NoInfer.key, + EnhancedNullability.ANNOTATION_CLASS_ID to EnhancedNullability.key, + ExtensionFunctionType.ANNOTATION_CLASS_ID to ExtensionFunctionType.key, + UnsafeVariance.ANNOTATION_CLASS_ID to UnsafeVariance.key ) - val classIdByCompilerAttribute: Map, ClassId> = compilerAttributeByClassId.entries.associateBy( + val classIdByCompilerAttributeKey: Map = compilerAttributeByClassId.entries.associateBy( keySelector = { it.value }, valueTransform = { it.key } ) - val compilerAttributeByFqName: Map> = compilerAttributeByClassId.mapKeys { it.key.asSingleFqName() } + val compilerAttributeKeyByFqName: Map = + compilerAttributeByClassId.mapKeys { it.key.asSingleFqName() } } val ConeAttributes.exact: CompilerConeAttributes.Exact? by ConeAttributes.attributeAccessor() diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeAttributes.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeAttributes.kt index 9f43777682a..869f197be84 100644 --- a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeAttributes.kt +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeAttributes.kt @@ -6,9 +6,9 @@ package org.jetbrains.kotlin.fir.types import org.jetbrains.kotlin.fir.util.ConeTypeRegistry +import org.jetbrains.kotlin.types.model.AnnotationMarker import org.jetbrains.kotlin.util.AttributeArrayOwner import org.jetbrains.kotlin.util.TypeRegistry -import org.jetbrains.kotlin.types.model.AnnotationMarker import org.jetbrains.kotlin.utils.addIfNotNull import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KClass @@ -34,6 +34,8 @@ abstract class ConeAttribute> : AnnotationMarker { abstract val key: KClass } +typealias ConeAttributeKey = KClass> + class ConeAttributes private constructor(attributes: List>) : AttributeArrayOwner, ConeAttribute<*>>(), Iterable> { @@ -82,7 +84,11 @@ class ConeAttributes private constructor(attributes: List>) : A } operator fun contains(attribute: ConeAttribute<*>): Boolean { - val index = getId(attribute.key) + return contains(attribute.key) + } + + operator fun contains(attributeKey: KClass>): Boolean { + val index = getId(attributeKey) return arrayMap[index] != null } diff --git a/compiler/fir/fir-serialization/src/org/jetbrains/kotlin/fir/serialization/FirElementSerializer.kt b/compiler/fir/fir-serialization/src/org/jetbrains/kotlin/fir/serialization/FirElementSerializer.kt index 92facff550a..268b393232e 100644 --- a/compiler/fir/fir-serialization/src/org/jetbrains/kotlin/fir/serialization/FirElementSerializer.kt +++ b/compiler/fir/fir-serialization/src/org/jetbrains/kotlin/fir/serialization/FirElementSerializer.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.fir.serialization +import org.jetbrains.kotlin.builtins.StandardNames import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.Modality @@ -17,11 +18,10 @@ import org.jetbrains.kotlin.fir.declarations.comparators.FirCallableDeclarationC import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyAccessor import org.jetbrains.kotlin.fir.declarations.utils.* import org.jetbrains.kotlin.fir.deserialization.projection -import org.jetbrains.kotlin.fir.expressions.FirAnnotation -import org.jetbrains.kotlin.fir.expressions.FirArgumentList -import org.jetbrains.kotlin.fir.expressions.FirExpression -import org.jetbrains.kotlin.fir.expressions.FirNamedArgumentExpression +import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.expressions.builder.buildAnnotation +import org.jetbrains.kotlin.fir.expressions.builder.buildAnnotationArgumentMapping +import org.jetbrains.kotlin.fir.expressions.builder.buildConstExpression import org.jetbrains.kotlin.fir.expressions.impl.FirEmptyAnnotationArgumentMapping import org.jetbrains.kotlin.fir.extensions.extensionService import org.jetbrains.kotlin.fir.extensions.typeAttributeExtensions @@ -51,6 +51,7 @@ import org.jetbrains.kotlin.name.* import org.jetbrains.kotlin.resolve.RequireKotlinConstants import org.jetbrains.kotlin.serialization.deserialization.ProtoEnumFlags import org.jetbrains.kotlin.types.AbstractTypeApproximator +import org.jetbrains.kotlin.types.ConstantValueKind import org.jetbrains.kotlin.types.TypeApproximatorConfiguration class FirElementSerializer private constructor( @@ -597,9 +598,9 @@ class FirElementSerializer private constructor( val compilerAttributes = mutableListOf>() val extensionAttributes = mutableListOf>() for (attribute in coneType.attributes) { - when (attribute) { - is CustomAnnotationTypeAttribute -> continue - in CompilerConeAttributes.classIdByCompilerAttribute -> compilerAttributes += attribute + when { + attribute is CustomAnnotationTypeAttribute -> continue + attribute.key in CompilerConeAttributes.classIdByCompilerAttributeKey -> compilerAttributes += attribute else -> extensionAttributes += attribute } } @@ -608,7 +609,7 @@ class FirElementSerializer private constructor( val annotation = buildAnnotation { annotationTypeRef = buildResolvedTypeRef { type = ConeClassLikeTypeImpl( - ConeClassLikeLookupTagImpl(CompilerConeAttributes.classIdByCompilerAttribute.getValue(attribute)), + ConeClassLikeLookupTagImpl(CompilerConeAttributes.classIdByCompilerAttributeKey.getValue(attribute.key)), emptyArray(), isNullable = false ) @@ -661,11 +662,6 @@ class FirElementSerializer private constructor( return functionType } fillFromPossiblyInnerType(builder, type) - if (type.isExtensionFunctionType) { - serializeAnnotationFromAttribute( - correspondingTypeRef?.annotations, CompilerConeAttributes.ExtensionFunctionType.ANNOTATION_CLASS_ID, builder - ) - } } is ConeTypeParameterType -> { val typeParameter = type.lookupTag.typeParameterSymbol.fir diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt index f52703fb480..9def9ffe1ff 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt @@ -408,8 +408,8 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo } private fun AnnotationMarker.isCustomAttribute(): Boolean { - val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttribute - return this !in compilerAttributes && this !is CustomAnnotationTypeAttribute + val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttributeKey + return (this as? ConeAttribute<*>)?.key !in compilerAttributes && this !is CustomAnnotationTypeAttribute } override fun KotlinTypeMarker.replaceCustomAttributes(newAttributes: List): KotlinTypeMarker { diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt index 7bdf8f82504..8d918054588 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt @@ -429,14 +429,14 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty override fun KotlinTypeMarker.hasCustomAttributes(): Boolean { require(this is ConeKotlinType) - val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttribute - return this.attributes.any { it !in compilerAttributes && it !is CustomAnnotationTypeAttribute } + val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttributeKey + return this.attributes.any { it.key !in compilerAttributes && it !is CustomAnnotationTypeAttribute } } override fun KotlinTypeMarker.getCustomAttributes(): List { require(this is ConeKotlinType) - val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttribute - return attributes.filterNot { it in compilerAttributes || it is CustomAnnotationTypeAttribute } + val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttributeKey + return attributes.filterNot { it.key in compilerAttributes || it is CustomAnnotationTypeAttribute } } override fun SimpleTypeMarker.isStubType(): Boolean { @@ -520,7 +520,7 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty override fun KotlinTypeMarker.hasAnnotation(fqName: FqName): Boolean { require(this is ConeKotlinType) - val compilerAttribute = CompilerConeAttributes.compilerAttributeByFqName[fqName] + val compilerAttribute = CompilerConeAttributes.compilerAttributeKeyByFqName[fqName] if (compilerAttribute != null) { return compilerAttribute in attributes }