All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.fir.declarations.FirDeclarationAttributes.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2020 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.declarations

import org.jetbrains.kotlin.fir.utils.AttributeArrayOwner
import org.jetbrains.kotlin.fir.utils.NullableArrayMapAccessor
import org.jetbrains.kotlin.fir.utils.TypeRegistry
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty

abstract class FirDeclarationDataKey

class FirDeclarationAttributes : AttributeArrayOwner() {
    override val typeRegistry: TypeRegistry
        get() = FirDeclarationDataRegistry

    internal operator fun set(key: KClass, value: Any?) {
        if (value == null) {
            removeComponent(key)
        } else {
            registerComponent(key, value)
        }
    }
}

/*
 * Example of adding new attribute for declaration:
 *
 *    object SomeKey : FirDeclarationDataKey()
 *    var FirDeclaration.someString: String? by FirDeclarationDataRegistry.data(SomeKey)
 */
object FirDeclarationDataRegistry : TypeRegistry() {
    fun  data(key: K): ReadWriteProperty {
        val kClass = key::class
        return DeclarationDataAccessor(generateNullableAccessor(kClass), kClass)
    }

    private class DeclarationDataAccessor(
        val dataAccessor: NullableArrayMapAccessor,
        val key: KClass
    ) : ReadWriteProperty {
        override fun getValue(thisRef: FirDeclaration, property: KProperty<*>): V? {
            return dataAccessor.getValue(thisRef.attributes, property)
        }

        override fun setValue(thisRef: FirDeclaration, property: KProperty<*>, value: V?) {
            thisRef.attributes[key] = value
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy