FIR: make ArrayMapOwner.arrayMap really protected

This commit is contained in:
Mikhail Glukhikh
2021-06-10 11:46:45 +03:00
parent 0972020324
commit bd39e457a7
8 changed files with 14 additions and 28 deletions
@@ -6,9 +6,7 @@
package org.jetbrains.kotlin.fir.types package org.jetbrains.kotlin.fir.types
import org.jetbrains.kotlin.fir.utils.AttributeArrayOwner import org.jetbrains.kotlin.fir.utils.AttributeArrayOwner
import org.jetbrains.kotlin.fir.utils.Protected
import org.jetbrains.kotlin.fir.utils.TypeRegistry import org.jetbrains.kotlin.fir.utils.TypeRegistry
import org.jetbrains.kotlin.fir.utils.isEmpty
import org.jetbrains.kotlin.types.model.AnnotationMarker import org.jetbrains.kotlin.types.model.AnnotationMarker
import org.jetbrains.kotlin.utils.addIfNotNull import org.jetbrains.kotlin.utils.addIfNotNull
import kotlin.properties.ReadOnlyProperty import kotlin.properties.ReadOnlyProperty
@@ -24,7 +22,6 @@ abstract class ConeAttribute<T : ConeAttribute<T>> : AnnotationMarker {
abstract val key: KClass<out T> abstract val key: KClass<out T>
} }
@OptIn(Protected::class)
class ConeAttributes private constructor(attributes: List<ConeAttribute<*>>) : AttributeArrayOwner<ConeAttribute<*>, ConeAttribute<*>>(), class ConeAttributes private constructor(attributes: List<ConeAttribute<*>>) : AttributeArrayOwner<ConeAttribute<*>, ConeAttribute<*>>(),
Iterable<ConeAttribute<*>> { Iterable<ConeAttribute<*>> {
@@ -85,16 +82,12 @@ class ConeAttributes private constructor(attributes: List<ConeAttribute<*>>) : A
} }
fun remove(attribute: ConeAttribute<*>): ConeAttributes { fun remove(attribute: ConeAttribute<*>): ConeAttributes {
if (arrayMap.isEmpty()) return this if (isEmpty()) return this
val attributes = arrayMap.filter { it != attribute } val attributes = arrayMap.filter { it != attribute }
if (attributes.size == arrayMap.size) return this if (attributes.size == arrayMap.size) return this
return create(attributes) return create(attributes)
} }
override fun iterator(): Iterator<ConeAttribute<*>> {
return arrayMap.iterator()
}
private inline fun perform(other: ConeAttributes, op: ConeAttribute<*>.(ConeAttribute<*>?) -> ConeAttribute<*>?): ConeAttributes { private inline fun perform(other: ConeAttributes, op: ConeAttribute<*>.(ConeAttribute<*>?) -> ConeAttribute<*>?): ConeAttributes {
if (this.isEmpty() && other.isEmpty()) return this if (this.isEmpty() && other.isEmpty()) return this
val attributes = mutableListOf<ConeAttribute<*>>() val attributes = mutableListOf<ConeAttribute<*>>()
@@ -109,8 +102,4 @@ class ConeAttributes private constructor(attributes: List<ConeAttribute<*>>) : A
override val typeRegistry: TypeRegistry<ConeAttribute<*>, ConeAttribute<*>> override val typeRegistry: TypeRegistry<ConeAttribute<*>, ConeAttribute<*>>
get() = Companion get() = Companion
private fun isEmpty(): Boolean {
return arrayMap.isEmpty()
}
} }
@@ -14,9 +14,6 @@ sealed class ArrayMap<T : Any> : Iterable<T> {
abstract fun copy(): ArrayMap<T> abstract fun copy(): ArrayMap<T>
} }
fun ArrayMap<*>.isEmpty(): Boolean = size == 0
fun ArrayMap<*>.isNotEmpty(): Boolean = size != 0
internal object EmptyArrayMap : ArrayMap<Nothing>() { internal object EmptyArrayMap : ArrayMap<Nothing>() {
override val size: Int override val size: Int
get() = 0 get() = 0
@@ -14,10 +14,8 @@ import kotlin.reflect.KProperty
@RequiresOptIn @RequiresOptIn
annotation class Protected annotation class Protected
@OptIn(Protected::class) abstract class AbstractArrayMapOwner<K : Any, V : Any> : Iterable<V> {
abstract class AbstractArrayMapOwner<K : Any, V : Any> { protected abstract val arrayMap: ArrayMap<V>
@Protected
abstract val arrayMap: ArrayMap<V>
protected abstract val typeRegistry: TypeRegistry<K, V> protected abstract val typeRegistry: TypeRegistry<K, V>
abstract class AbstractArrayMapAccessor<K : Any, V : Any, T : V>( abstract class AbstractArrayMapAccessor<K : Any, V : Any, T : V>(
@@ -31,6 +29,14 @@ abstract class AbstractArrayMapOwner<K : Any, V : Any> {
} }
protected abstract fun registerComponent(tClass: KClass<out K>, value: V) protected abstract fun registerComponent(tClass: KClass<out K>, value: V)
final override fun iterator(): Iterator<V> = arrayMap.iterator()
fun isEmpty(): Boolean = arrayMap.size == 0
fun isNotEmpty(): Boolean = arrayMap.size != 0
operator fun get(index: Int): V? = arrayMap[index]
} }
class ArrayMapAccessor<K : Any, V : Any, T : V>( class ArrayMapAccessor<K : Any, V : Any, T : V>(
@@ -16,7 +16,6 @@ import kotlin.reflect.KClass
* Note that you can remove attributes from [AttributeArrayOwner] despite * Note that you can remove attributes from [AttributeArrayOwner] despite
* from components in [ComponentArrayOwner] * from components in [ComponentArrayOwner]
*/ */
@OptIn(Protected::class)
abstract class AttributeArrayOwner<K : Any, T : Any> protected constructor( abstract class AttributeArrayOwner<K : Any, T : Any> protected constructor(
arrayMap: ArrayMap<T> arrayMap: ArrayMap<T>
) : AbstractArrayMapOwner<K, T>() { ) : AbstractArrayMapOwner<K, T>() {
@@ -11,7 +11,6 @@ import kotlin.reflect.KClass
* [ComponentArrayOwner] based on [ArrayMap] with flexible size and should be used for * [ComponentArrayOwner] based on [ArrayMap] with flexible size and should be used for
* storing services in entities with limited number of instances, like FirSession * storing services in entities with limited number of instances, like FirSession
*/ */
@OptIn(Protected::class)
abstract class ComponentArrayOwner<K : Any, V : Any> : AbstractArrayMapOwner<K, V>() { abstract class ComponentArrayOwner<K : Any, V : Any> : AbstractArrayMapOwner<K, V>() {
final override val arrayMap: ArrayMap<V> = final override val arrayMap: ArrayMap<V> =
ArrayMapImpl() ArrayMapImpl()
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.fir.FirSessionComponent
import org.jetbrains.kotlin.fir.NoMutableState import org.jetbrains.kotlin.fir.NoMutableState
import org.jetbrains.kotlin.fir.utils.ArrayMapAccessor import org.jetbrains.kotlin.fir.utils.ArrayMapAccessor
import org.jetbrains.kotlin.fir.utils.ComponentArrayOwner import org.jetbrains.kotlin.fir.utils.ComponentArrayOwner
import org.jetbrains.kotlin.fir.utils.Protected
import org.jetbrains.kotlin.fir.utils.TypeRegistry import org.jetbrains.kotlin.fir.utils.TypeRegistry
import kotlin.reflect.KClass import kotlin.reflect.KClass
@@ -50,7 +49,6 @@ class FirExtensionService(val session: FirSession) : ComponentArrayOwner<FirExte
} }
@PluginServicesInitialization @PluginServicesInitialization
@OptIn(Protected::class)
fun getAllExtensions(): List<FirExtension> { fun getAllExtensions(): List<FirExtension> {
return arrayMap.flatten() return arrayMap.flatten()
} }
@@ -377,7 +377,7 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty
override fun KotlinTypeMarker.getAnnotations(): List<AnnotationMarker> { override fun KotlinTypeMarker.getAnnotations(): List<AnnotationMarker> {
require(this is ConeKotlinType) require(this is ConeKotlinType)
return attributes.arrayMap.toList() return attributes.toList()
} }
override fun SimpleTypeMarker.isStubType(): Boolean { override fun SimpleTypeMarker.isStubType(): Boolean {
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.utils.isNotEmpty
import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid
import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
@@ -413,7 +412,7 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
} }
private fun FirDeclaration.renderDeclarationAttributesIfNeeded() { private fun FirDeclaration.renderDeclarationAttributesIfNeeded() {
if (mode.renderDeclarationAttributes && attributes.arrayMap.isNotEmpty()) { if (mode.renderDeclarationAttributes && attributes.isNotEmpty()) {
val attributes = getAttributesWithValues().mapNotNull { (klass, value) -> val attributes = getAttributesWithValues().mapNotNull { (klass, value) ->
value?.let { klass.simpleName to value.renderAsDeclarationAttributeValue() } value?.let { klass.simpleName to value.renderAsDeclarationAttributeValue() }
}.joinToString { (name, value) -> "$name=$value" } }.joinToString { (name, value) -> "$name=$value" }
@@ -427,10 +426,9 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
} }
} }
private fun FirDeclaration.getAttributesWithValues(): List<Pair<KClass<out FirDeclarationDataKey>, Any?>> { private fun FirDeclaration.getAttributesWithValues(): List<Pair<KClass<out FirDeclarationDataKey>, Any?>> {
val attributesMap = FirDeclarationDataRegistry.allValuesThreadUnsafeForRendering() val attributesMap = FirDeclarationDataRegistry.allValuesThreadUnsafeForRendering()
return attributesMap.entries.sortedBy { it.key.simpleName }.map { (klass, index) -> klass to attributes.arrayMap[index] } return attributesMap.entries.sortedBy { it.key.simpleName }.map { (klass, index) -> klass to attributes[index] }
} }
private fun Any.renderAsDeclarationAttributeValue() = when (this) { private fun Any.renderAsDeclarationAttributeValue() = when (this) {