Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.resolve.constants
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.findClassAcrossModuleDependencies
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
abstract class ConstantValue(open val value: T) {
abstract fun getType(module: ModuleDescriptor): KotlinType
abstract fun accept(visitor: AnnotationArgumentVisitor, data: D): R
override fun equals(other: Any?): Boolean = this === other || value == (other as? ConstantValue<*>)?.value
override fun hashCode(): Int = value?.hashCode() ?: 0
override fun toString(): String = value.toString()
open fun stringTemplateValue(): String = value.toString()
}
abstract class IntegerValueConstant protected constructor(value: T) : ConstantValue(value)
abstract class UnsignedValueConstant protected constructor(value: T) : ConstantValue(value)
class AnnotationValue(value: AnnotationDescriptor) : ConstantValue(value) {
override fun getType(module: ModuleDescriptor): KotlinType = value.type
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitAnnotationValue(this, data)
}
class ArrayValue(
value: List>,
private val computeType: (ModuleDescriptor) -> KotlinType
) : ConstantValue>>(value) {
override fun getType(module: ModuleDescriptor): KotlinType = computeType(module).also { type ->
assert(KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type)) { "Type should be an array, but was $type: $value" }
}
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitArrayValue(this, data)
}
class BooleanValue(value: Boolean) : ConstantValue(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.booleanType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitBooleanValue(this, data)
}
class ByteValue(value: Byte) : IntegerValueConstant(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.byteType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitByteValue(this, data)
override fun toString(): String = "$value.toByte()"
}
class CharValue(value: Char) : IntegerValueConstant(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.charType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitCharValue(this, data)
override fun toString() = "\\u%04X ('%s')".format(value.toInt(), getPrintablePart(value))
private fun getPrintablePart(c: Char): String = when (c) {
'\b' -> "\\b"
'\t' -> "\\t"
'\n' -> "\\n"
//TODO: KT-8507
12.toChar() -> "\\f"
'\r' -> "\\r"
else -> if (isPrintableUnicode(c)) Character.toString(c) else "?"
}
private fun isPrintableUnicode(c: Char): Boolean {
val t = Character.getType(c).toByte()
return t != Character.UNASSIGNED &&
t != Character.LINE_SEPARATOR &&
t != Character.PARAGRAPH_SEPARATOR &&
t != Character.CONTROL &&
t != Character.FORMAT &&
t != Character.PRIVATE_USE &&
t != Character.SURROGATE
}
}
class DoubleValue(value: Double) : ConstantValue(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.doubleType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitDoubleValue(this, data)
override fun toString() = "$value.toDouble()"
}
class EnumValue(val enumClassId: ClassId, val enumEntryName: Name) : ConstantValue>(enumClassId to enumEntryName) {
override fun getType(module: ModuleDescriptor): KotlinType =
module.findClassAcrossModuleDependencies(enumClassId)?.takeIf(DescriptorUtils::isEnumClass)?.defaultType
?: ErrorUtils.createErrorType("Containing class for error-class based enum entry $enumClassId.$enumEntryName")
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitEnumValue(this, data)
override fun toString() = "${enumClassId.shortClassName}.$enumEntryName"
}
abstract class ErrorValue : ConstantValue(Unit) {
@Deprecated("Should not be called, for this is not a real value, but a indication of an error")
override val value: Unit
get() = throw UnsupportedOperationException()
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitErrorValue(this, data)
class ErrorValueWithMessage(val message: String) : ErrorValue() {
override fun getType(module: ModuleDescriptor) = ErrorUtils.createErrorType(message)
override fun toString() = message
}
companion object {
fun create(message: String): ErrorValue {
return ErrorValueWithMessage(message)
}
}
}
class FloatValue(value: Float) : ConstantValue(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.floatType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitFloatValue(this, data)
override fun toString() = "$value.toFloat()"
}
class IntValue(value: Int) : IntegerValueConstant(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.intType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitIntValue(this, data)
}
class KClassValue(private val type: KotlinType) : ConstantValue(type) {
override fun getType(module: ModuleDescriptor): KotlinType = type
override val value: KotlinType
get() = type.arguments.single().type
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitKClassValue(this, data)
}
class LongValue(value: Long) : IntegerValueConstant(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.longType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitLongValue(this, data)
override fun toString() = "$value.toLong()"
}
class NullValue : ConstantValue(null) {
override fun getType(module: ModuleDescriptor) = module.builtIns.nullableNothingType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitNullValue(this, data)
}
class ShortValue(value: Short) : IntegerValueConstant(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.shortType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitShortValue(this, data)
override fun toString() = "$value.toShort()"
}
class StringValue(value: String) : ConstantValue(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.stringType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitStringValue(this, data)
override fun toString() = "\"$value\""
}
class UByteValue(byteValue: Byte) : UnsignedValueConstant(byteValue) {
override fun getType(module: ModuleDescriptor): KotlinType {
return module.findClassAcrossModuleDependencies(KotlinBuiltIns.FQ_NAMES.uByte)?.defaultType
?: ErrorUtils.createErrorType("Unsigned type UByte not found")
}
override fun accept(visitor: AnnotationArgumentVisitor, data: D): R = visitor.visitUByteValue(this, data)
override fun toString() = "$value.toUByte()"
override fun stringTemplateValue(): String = (value.toInt() and 0xFF).toString()
}
class UShortValue(shortValue: Short) : UnsignedValueConstant(shortValue) {
override fun getType(module: ModuleDescriptor): KotlinType {
return module.findClassAcrossModuleDependencies(KotlinBuiltIns.FQ_NAMES.uShort)?.defaultType
?: ErrorUtils.createErrorType("Unsigned type UShort not found")
}
override fun accept(visitor: AnnotationArgumentVisitor, data: D): R = visitor.visitUShortValue(this, data)
override fun toString() = "$value.toUShort()"
override fun stringTemplateValue(): String = (value.toInt() and 0xFFFF).toString()
}
class UIntValue(intValue: Int) : UnsignedValueConstant(intValue) {
override fun getType(module: ModuleDescriptor): KotlinType {
return module.findClassAcrossModuleDependencies(KotlinBuiltIns.FQ_NAMES.uInt)?.defaultType
?: ErrorUtils.createErrorType("Unsigned type UInt not found")
}
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitUIntValue(this, data)
override fun toString() = "$value.toUInt()"
override fun stringTemplateValue(): String = (value.toLong() and 0xFFFFFFFFL).toString()
}
class ULongValue(longValue: Long) : UnsignedValueConstant(longValue) {
override fun getType(module: ModuleDescriptor): KotlinType {
return module.findClassAcrossModuleDependencies(KotlinBuiltIns.FQ_NAMES.uLong)?.defaultType
?: ErrorUtils.createErrorType("Unsigned type ULong not found")
}
override fun accept(visitor: AnnotationArgumentVisitor, data: D): R = visitor.visitULongValue(this, data)
override fun toString() = "$value.toULong()"
override fun stringTemplateValue(): String {
if (value >= 0) return value.toString()
val div10 = (value ushr 1) / 5
val mod10 = value - 10 * div10
return "$div10$mod10"
}
}