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

.kotlin.kotlin-compiler.1.3.11.source-code.InliningContext.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2010-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.kotlin.codegen.inline

import org.jetbrains.kotlin.codegen.ClassBuilder
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.psi.KtElement

class RootInliningContext(
        expressionMap: Map,
        state: GenerationState,
        nameGenerator: NameGenerator,
        val sourceCompilerForInline: SourceCompilerForInline,
        override val callSiteInfo: InlineCallSiteInfo,
        val inlineMethodReifier: ReifiedTypeInliner,
        typeParameterMappings: TypeParameterMappings
) : InliningContext(
        null, expressionMap, state, nameGenerator, TypeRemapper.createRoot(typeParameterMappings), null, false
)

class RegeneratedClassContext(
        parent: InliningContext,
        expressionMap: Map,
        state: GenerationState,
        nameGenerator: NameGenerator,
        typeRemapper: TypeRemapper,
        lambdaInfo: LambdaInfo?,
        override val callSiteInfo: InlineCallSiteInfo
) : InliningContext(
        parent, expressionMap, state, nameGenerator, typeRemapper, lambdaInfo, true
) {
    val continuationBuilders: MutableMap = hashMapOf()
}

open class InliningContext(
        val parent: InliningContext?,
        val expressionMap: Map,
        val state: GenerationState,
        val nameGenerator: NameGenerator,
        val typeRemapper: TypeRemapper,
        val lambdaInfo: LambdaInfo?,
        val classRegeneration: Boolean
) {

    val isInliningLambda = lambdaInfo != null

    private val internalNameToAnonymousObjectTransformationInfo = hashMapOf()

    var isContinuation: Boolean = false

    val isRoot: Boolean = parent == null

    val root: RootInliningContext
        get() = if (isRoot) this as RootInliningContext else parent!!.root

    fun findAnonymousObjectTransformationInfo(internalName: String, searchInParent: Boolean = true): AnonymousObjectTransformationInfo? =
        internalNameToAnonymousObjectTransformationInfo[internalName]
                ?: if (searchInParent) parent?.findAnonymousObjectTransformationInfo(internalName, searchInParent) else null

    fun recordIfNotPresent(internalName: String, info: AnonymousObjectTransformationInfo) {
        internalNameToAnonymousObjectTransformationInfo.putIfAbsent(internalName, info)
    }

    fun subInlineLambda(lambdaInfo: LambdaInfo): InliningContext =
            subInline(
                    nameGenerator.subGenerator("lambda"),
                    //mark lambda inlined
                    hashMapOf(lambdaInfo.lambdaClassType.internalName to null),
                    lambdaInfo
            )

    fun subInlineWithClassRegeneration(
            generator: NameGenerator,
            newTypeMappings: MutableMap,
            callSiteInfo: InlineCallSiteInfo
    ): InliningContext = RegeneratedClassContext(
            this, expressionMap, state, generator, TypeRemapper.createFrom(typeRemapper, newTypeMappings),
            lambdaInfo, callSiteInfo
    )

    @JvmOverloads
    fun subInline(
            generator: NameGenerator,
            additionalTypeMappings: Map = emptyMap(),
            lambdaInfo: LambdaInfo? = this.lambdaInfo
    ): InliningContext {
        val isInliningLambda = lambdaInfo != null
        return InliningContext(
                this, expressionMap, state, generator,
                TypeRemapper.createFrom(
                        typeRemapper,
                        additionalTypeMappings,
                        //root inline lambda
                        isInliningLambda && !this.isInliningLambda
                ),
                lambdaInfo, classRegeneration
        )
    }

    open val callSiteInfo: InlineCallSiteInfo
        get() {
            return parent!!.callSiteInfo
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy