kotlin.reflect.jvm.internal.impl.resolve.constants.CompileTimeConstant.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-reflect Show documentation
Show all versions of kotlin-reflect Show documentation
Kotlin Full Reflection Library
/*
* Copyright 2010-2016 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.*
interface CompileTimeConstant {
val isError: Boolean
get() = false
val parameters: CompileTimeConstant.Parameters
fun toConstantValue(expectedType: KotlinType): ConstantValue
fun getValue(expectedType: KotlinType): T = toConstantValue(expectedType).value
val canBeUsedInAnnotations: Boolean get() = parameters.canBeUsedInAnnotation
val usesVariableAsConstant: Boolean get() = parameters.usesVariableAsConstant
val usesNonConstValAsConstant: Boolean get() = parameters.usesNonConstValAsConstant
val isPure: Boolean get() = parameters.isPure
class Parameters(
val canBeUsedInAnnotation: Boolean,
val isPure: Boolean,
val usesVariableAsConstant: Boolean,
val usesNonConstValAsConstant: Boolean
)
}
class TypedCompileTimeConstant(
val constantValue: ConstantValue,
override val parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant {
override val isError: Boolean
get() = constantValue is ErrorValue
val type: KotlinType = constantValue.type
override fun toConstantValue(expectedType: KotlinType): ConstantValue = constantValue
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is TypedCompileTimeConstant<*>) return false
if (isError) return other.isError
if (other.isError) return false
return constantValue.value == other.constantValue.value && type == other.type
}
override fun hashCode(): Int {
if (isError) return 13
var result = constantValue.value?.hashCode() ?: 0
result = 31 * result + type.hashCode()
return result
}
}
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 = KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(Annotations.EMPTY, typeConstructor, emptyList(),
false, ErrorUtils.createErrorScope("Scope for number value type (" + typeConstructor.toString() + ")", true)
)
fun getType(expectedType: KotlinType): KotlinType = TypeUtils.getPrimitiveNumberType(typeConstructor, expectedType)
override fun toString() = typeConstructor.toString()
override fun equals(other: Any?) = other is IntegerValueTypeConstant && value == other.value
override fun hashCode() = value.hashCode()
}