FIR: support custom annotation-based type attributes

This commit is contained in:
Mikhail Glukhikh
2021-01-20 13:56:00 +03:00
parent 4cd6266bce
commit 94e613dd01
8 changed files with 50 additions and 9 deletions
@@ -0,0 +1,27 @@
/*
* Copyright 2010-2021 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.fir.types
import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall
import org.jetbrains.kotlin.fir.render
import kotlin.reflect.KClass
class CustomAnnotationTypeAttribute(val annotations: List<FirAnnotationCall>) : ConeAttribute<CustomAnnotationTypeAttribute>() {
override fun union(other: CustomAnnotationTypeAttribute?): CustomAnnotationTypeAttribute? = null
override fun intersect(other: CustomAnnotationTypeAttribute?): CustomAnnotationTypeAttribute? = null
override fun isSubtypeOf(other: CustomAnnotationTypeAttribute?): Boolean = true
override fun toString(): String = annotations.joinToString(separator = " ") { it.render() }
override val key: KClass<out CustomAnnotationTypeAttribute>
get() = CustomAnnotationTypeAttribute::class
}
private val ConeAttributes.custom: CustomAnnotationTypeAttribute? by ConeAttributes.attributeAccessor<CustomAnnotationTypeAttribute>()
val ConeAttributes.customAnnotations: List<FirAnnotationCall> get() = custom?.annotations.orEmpty()
@@ -95,6 +95,7 @@ fun List<FirAnnotationCall>.computeTypeAttributes(
): ConeAttributes {
if (this.isEmpty()) return ConeAttributes.Empty
val attributes = mutableListOf<ConeAttribute<*>>()
val customAnnotations = mutableListOf<FirAnnotationCall>()
for (annotation in this) {
val type = annotation.annotationTypeRef.coneTypeSafe<ConeClassLikeType>() ?: continue
when (val classId = type.lookupTag.classId) {
@@ -102,9 +103,18 @@ fun List<FirAnnotationCall>.computeTypeAttributes(
CompilerConeAttributes.NoInfer.ANNOTATION_CLASS_ID -> attributes += CompilerConeAttributes.NoInfer
CompilerConeAttributes.ExtensionFunctionType.ANNOTATION_CLASS_ID -> attributes += CompilerConeAttributes.ExtensionFunctionType
CompilerConeAttributes.UnsafeVariance.ANNOTATION_CLASS_ID -> attributes += CompilerConeAttributes.UnsafeVariance
else -> additionalProcessor.invoke(attributes, classId)
else -> {
if (classId.startsWith(StandardClassIds.BASE_KOTLIN_PACKAGE.shortName())) {
// The check ^ is intended to leave only annotations which may be important for BE
customAnnotations += annotation
}
additionalProcessor.invoke(attributes, classId)
}
}
}
if (customAnnotations.isNotEmpty()) {
attributes += CustomAnnotationTypeAttribute(customAnnotations)
}
return ConeAttributes.create(attributes)
}