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

org.jetbrains.kotlin.fir.java.declarations.FirJavaField.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2023 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.java.declarations

import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.FirImplementationDetail
import org.jetbrains.kotlin.fir.FirModuleData
import org.jetbrains.kotlin.fir.builder.FirBuilderDsl
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.builder.FirFieldBuilder
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.references.FirControlFlowGraphReference
import org.jetbrains.kotlin.fir.symbols.impl.FirFieldSymbol
import org.jetbrains.kotlin.fir.types.ConeSimpleKotlinType
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.visitors.FirTransformer
import org.jetbrains.kotlin.fir.visitors.FirVisitor
import org.jetbrains.kotlin.fir.visitors.transformInplace
import org.jetbrains.kotlin.fir.visitors.transformSingle
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.properties.Delegates

class FirJavaField @FirImplementationDetail constructor(
    override val source: KtSourceElement?,
    override val moduleData: FirModuleData,
    override val origin: FirDeclarationOrigin.Java,
    override val symbol: FirFieldSymbol,
    override val name: Name,
    resolvePhase: FirResolvePhase,
    override var returnTypeRef: FirTypeRef,
    override var status: FirDeclarationStatus,
    override val isVar: Boolean,
    annotationBuilder: () -> List,
    override val typeParameters: MutableList,
    lazyInitializer: Lazy,
    lazyHasConstantInitializer: Lazy,
    override val dispatchReceiverType: ConeSimpleKotlinType?,
    override val attributes: FirDeclarationAttributes,
) : FirField() {
    internal var lazyInitializer: Lazy = lazyInitializer
        private set

    internal var lazyHasConstantInitializer: Lazy = lazyHasConstantInitializer
        private set

    init {
        symbol.bind(this)

        @OptIn(ResolveStateAccess::class)
        this.resolveState = resolvePhase.asResolveState()
    }

    override val receiverParameter: FirReceiverParameter? get() = null
    override val isVal: Boolean get() = !isVar
    override val getter: FirPropertyAccessor? get() = null
    override val setter: FirPropertyAccessor? get() = null
    override val backingField: FirBackingField? = null
    override val controlFlowGraphReference: FirControlFlowGraphReference? get() = null

    override val annotations: List by lazy { annotationBuilder() }

    override val initializer: FirExpression?
        get() = lazyInitializer.value

    override val hasConstantInitializer: Boolean
        get() = lazyHasConstantInitializer.value

    override val deprecationsProvider: DeprecationsProvider by lazy {
        annotations.getDeprecationsProviderFromAnnotations(moduleData.session, fromJava = true)
    }

    override val contextReceivers: List
        get() = emptyList()

    override fun  transformReturnTypeRef(transformer: FirTransformer, data: D): FirField {
        returnTypeRef = returnTypeRef.transformSingle(transformer, data)
        return this
    }

    override fun  transformGetter(transformer: FirTransformer, data: D): FirField {
        return this
    }

    override fun  transformSetter(transformer: FirTransformer, data: D): FirField {
        return this
    }

    override fun  transformBackingField(transformer: FirTransformer, data: D): FirField {
        return this
    }

    override fun  transformOtherChildren(transformer: FirTransformer, data: D): FirField {
        transformAnnotations(transformer, data)
        replaceInitializer(initializer?.transformSingle(transformer, data))
        return this
    }

    override fun  transformReceiverParameter(transformer: FirTransformer, data: D): FirField {
        return this
    }

    override fun  acceptChildren(visitor: FirVisitor, data: D) {
        returnTypeRef.accept(visitor, data)
        annotations.forEach { it.accept(visitor, data) }
        typeParameters.forEach { it.accept(visitor, data) }
        initializer?.accept(visitor, data)
    }

    override fun  transformChildren(transformer: FirTransformer, data: D): FirJavaField {
        transformReturnTypeRef(transformer, data)
        transformTypeParameters(transformer, data)
        transformOtherChildren(transformer, data)
        return this
    }

    override fun  transformStatus(transformer: FirTransformer, data: D): FirJavaField {
        status = status.transformSingle(transformer, data)
        return this
    }

    override fun replaceReturnTypeRef(newReturnTypeRef: FirTypeRef) {
        returnTypeRef = newReturnTypeRef
    }

    override fun replaceAnnotations(newAnnotations: List) {
        throw AssertionError("Mutating annotations for FirJava* is not supported")
    }

    override fun  transformAnnotations(transformer: FirTransformer, data: D): FirJavaField {
        return this
    }

    override fun replaceInitializer(newInitializer: FirExpression?) {
        lazyInitializer = lazyOf(newInitializer)
    }

    override fun  transformTypeParameters(transformer: FirTransformer, data: D): FirField {
        typeParameters.transformInplace(transformer, data)
        return this
    }

    override fun replaceDelegate(newDelegate: FirExpression?) {}
    override val delegate: FirExpression?
        get() = null

    override var containerSource: DeserializedContainerSource? = null

    override fun  transformInitializer(transformer: FirTransformer, data: D): FirField {
        return this
    }

    override fun replaceReceiverParameter(newReceiverParameter: FirReceiverParameter?) {}
    override fun replaceDeprecationsProvider(newDeprecationsProvider: DeprecationsProvider) {}

    override fun  transformDelegate(transformer: FirTransformer, data: D): FirField {
        return this
    }

    override fun replaceGetter(newGetter: FirPropertyAccessor?) {}

    override fun replaceSetter(newSetter: FirPropertyAccessor?) {}

    override fun replaceControlFlowGraphReference(newControlFlowGraphReference: FirControlFlowGraphReference?) {}

    override fun replaceContextReceivers(newContextReceivers: List) {
        error("Body cannot be replaced for FirJavaField")
    }

    override fun replaceStatus(newStatus: FirDeclarationStatus) {
        status = newStatus
    }
}

@FirBuilderDsl
internal class FirJavaFieldBuilder : FirFieldBuilder() {
    var modality: Modality? = null
    lateinit var visibility: Visibility
    var isFromSource: Boolean by Delegates.notNull()
    lateinit var annotationBuilder: () -> List
    var lazyInitializer: Lazy? = null
    lateinit var lazyHasConstantInitializer: Lazy

    override var resolvePhase: FirResolvePhase = FirResolvePhase.ANALYZED_DEPENDENCIES

    @OptIn(FirImplementationDetail::class)
    override fun build(): FirJavaField {
        return FirJavaField(
            source,
            moduleData,
            origin = javaOrigin(isFromSource),
            symbol,
            name,
            resolvePhase,
            returnTypeRef,
            status,
            isVar,
            annotationBuilder,
            typeParameters,
            lazyInitializer ?: lazyOf(initializer),
            lazyHasConstantInitializer,
            dispatchReceiverType,
            attributes,
        )
    }

    @Deprecated("Modification of 'origin' has no impact for FirJavaFieldBuilder", level = DeprecationLevel.HIDDEN)
    override var origin: FirDeclarationOrigin
        get() = throw IllegalStateException()
        set(@Suppress("UNUSED_PARAMETER") value) {
            throw IllegalStateException()
        }
}

@OptIn(ExperimentalContracts::class)
internal inline fun buildJavaField(init: FirJavaFieldBuilder.() -> Unit): FirJavaField {
    contract {
        callsInPlace(init, InvocationKind.EXACTLY_ONCE)
    }
    return FirJavaFieldBuilder().apply(init).build()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy