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

org.jetbrains.kotlin.backend.common.Mappings.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2022 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.backend.common

import org.jetbrains.kotlin.ir.declarations.*
import java.util.concurrent.ConcurrentHashMap
import kotlin.reflect.KMutableProperty0
import kotlin.reflect.KProperty

interface Mapping {
    val defaultArgumentsDispatchFunction: Delegate
    val defaultArgumentsOriginalFunction: Delegate
    val suspendFunctionToCoroutineConstructor: Delegate
    val lateInitFieldToNullableField: Delegate
    val inlineClassMemberToStatic: Delegate
    val capturedFields: Delegate>
    val capturedConstructors: Delegate
    val reflectedNameAccessor: Delegate
    val suspendFunctionsToFunctionWithContinuations: Delegate
    val functionWithContinuationsToSuspendFunctions: Delegate

    abstract class Delegate {
        abstract operator fun get(key: K): V?

        abstract operator fun set(key: K, value: V?)

        operator fun getValue(thisRef: K, desc: KProperty<*>): V? = get(thisRef)

        operator fun setValue(thisRef: K, desc: KProperty<*>, value: V?) {
            set(thisRef, value)
        }

        abstract val keys: Set
    }
}

interface DelegateFactory {
    fun  newDeclarationToDeclarationMapping(): Mapping.Delegate

    fun > newDeclarationToDeclarationCollectionMapping(): Mapping.Delegate
}

object DefaultDelegateFactory : DelegateFactory {
    fun  newDeclarationToValueMapping(): Mapping.Delegate = newMappingImpl()

    override fun  newDeclarationToDeclarationMapping(): Mapping.Delegate = newMappingImpl()

    override fun > newDeclarationToDeclarationCollectionMapping(): Mapping.Delegate = newMappingImpl()

    private fun  newMappingImpl() = object : Mapping.Delegate() {
        private val map: MutableMap = ConcurrentHashMap()

        override operator fun get(key: K): V? {
            return map[key]
        }

        override operator fun set(key: K, value: V?) {
            if (value == null) {
                map.remove(key)
            } else {
                map[key] = value
            }
        }

        override val keys: Set
            get() = map.keys
    }
}

open class DefaultMapping(delegateFactory: DelegateFactory = DefaultDelegateFactory) : Mapping {
    override val defaultArgumentsDispatchFunction: Mapping.Delegate = delegateFactory.newDeclarationToDeclarationMapping()
    override val defaultArgumentsOriginalFunction: Mapping.Delegate = delegateFactory.newDeclarationToDeclarationMapping()
    override val suspendFunctionToCoroutineConstructor: Mapping.Delegate = delegateFactory.newDeclarationToDeclarationMapping()
    override val lateInitFieldToNullableField: Mapping.Delegate = delegateFactory.newDeclarationToDeclarationMapping()
    override val inlineClassMemberToStatic: Mapping.Delegate = delegateFactory.newDeclarationToDeclarationMapping()
    override val capturedFields: Mapping.Delegate> = delegateFactory.newDeclarationToDeclarationCollectionMapping()
    override val capturedConstructors: Mapping.Delegate = delegateFactory.newDeclarationToDeclarationMapping()
    override val reflectedNameAccessor: Mapping.Delegate = delegateFactory.newDeclarationToDeclarationMapping()
    override val suspendFunctionsToFunctionWithContinuations: Mapping.Delegate = delegateFactory.newDeclarationToDeclarationMapping()
    override val functionWithContinuationsToSuspendFunctions: Mapping.Delegate = delegateFactory.newDeclarationToDeclarationMapping()
}

fun  KMutableProperty0.getOrPut(fn: () -> V) = this.get() ?: fn().also {
    this.set(it)
}

fun  Mapping.Delegate.getOrPut(key: K, fn: () -> V) = this[key] ?: fn().also {
    this[key] = it
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy