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

org.jetbrains.kotlin.codegen.when.SwitchCodegen.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2000-2018 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.codegen.`when`

import org.jetbrains.kotlin.cfg.WhenChecker
import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.psi.KtWhenEntry
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.constants.ConstantValue
import org.jetbrains.kotlin.resolve.constants.NullValue
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
import java.util.*
import kotlin.collections.ArrayList

@Suppress("MemberVisibilityCanBePrivate")
abstract class SwitchCodegen(
    @JvmField
    protected val expression: KtWhenExpression,
    protected val isStatement: Boolean,
    private val isExhaustive: Boolean,
    @JvmField
    protected val codegen: ExpressionCodegen,
    subjectType: Type?
) {
    protected val bindingContext: BindingContext = codegen.bindingContext

    protected val subjectVariable = expression.subjectVariable
    protected val subjectExpression = expression.subjectExpression ?: throw AssertionError("No subject expression: ${expression.text}")

    protected val subjectKotlinType = WhenChecker.whenSubjectTypeWithoutSmartCasts(expression, bindingContext)
        ?: throw AssertionError("No subject type: $expression")

    @JvmField
    protected val subjectType = subjectType ?: codegen.asmType(subjectKotlinType)

    protected var subjectLocal = -1

    protected val resultKotlinType: KotlinType? = if (!isStatement) codegen.kotlinType(expression) else null

    protected val resultType: Type = if (isStatement) Type.VOID_TYPE else codegen.expressionType(expression)

    @JvmField
    protected val v: InstructionAdapter = codegen.v

    @JvmField
    protected val transitionsTable: NavigableMap = TreeMap()

    private val entryLabels: MutableList




© 2015 - 2024 Weber Informatics LLC | Privacy Policy