kotlin.reflect.jvm.internal.impl.resolve.constants.CompileTimeConstant.kt Maven / Gradle / Ivy
/*
* 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 kotlin.reflect.jvm.internal.impl.resolve.constants
import kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns
import kotlin.reflect.jvm.internal.impl.descriptors.annotations.Annotations
import kotlin.reflect.jvm.internal.impl.types.*
public interface CompileTimeConstant {
public val isError: Boolean
get() = false
public val parameters: CompileTimeConstant.Parameters
public fun toConstantValue(expectedType: KotlinType): ConstantValue
public fun getValue(expectedType: KotlinType): T = toConstantValue(expectedType).value
public val canBeUsedInAnnotations: Boolean get() = parameters.canBeUsedInAnnotation
public val usesVariableAsConstant: Boolean get() = parameters.usesVariableAsConstant
public val usesNonConstValAsConstant: Boolean get() = parameters.usesNonConstValAsConstant
public val isPure: Boolean get() = parameters.isPure
public class Parameters(
public val canBeUsedInAnnotation: Boolean,
public val isPure: Boolean,
public val usesVariableAsConstant: Boolean,
public val usesNonConstValAsConstant: Boolean
)
}
public class TypedCompileTimeConstant(
public val constantValue: ConstantValue,
override val parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant {
override val isError: Boolean
get() = constantValue is ErrorValue
public val type: KotlinType = constantValue.type
override fun toConstantValue(expectedType: KotlinType): ConstantValue = constantValue
}
public class IntegerValueTypeConstant(
private val value: Number,
private val builtIns: KotlinBuiltIns,
override val parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant {
private val typeConstructor = IntegerValueTypeConstructor(value.toLong(), builtIns)
override fun toConstantValue(expectedType: KotlinType): ConstantValue {
val factory = ConstantValueFactory(builtIns)
val type = getType(expectedType)
return when {
KotlinBuiltIns.isInt(type) -> {
factory.createIntValue(value.toInt())
}
KotlinBuiltIns.isByte(type) -> {
factory.createByteValue(value.toByte())
}
KotlinBuiltIns.isShort(type) -> {
factory.createShortValue(value.toShort())
}
else -> {
factory.createLongValue(value.toLong())
}
}
}
val unknownIntegerType = KotlinTypeImpl.create(
Annotations.EMPTY, typeConstructor, false, emptyList(),
ErrorUtils.createErrorScope("Scope for number value type (" + typeConstructor.toString() + ")", true)
)
public fun getType(expectedType: KotlinType): KotlinType = TypeUtils.getPrimitiveNumberType(typeConstructor, expectedType)
override fun toString() = typeConstructor.toString()
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy