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

kotlin.reflect.jvm.internal.impl.resolve.constants.constantValues.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 kotlin.reflect.jvm.internal.impl.resolve.constants

import kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns
import kotlin.reflect.jvm.internal.impl.descriptors.ModuleDescriptor
import kotlin.reflect.jvm.internal.impl.descriptors.annotations.AnnotationArgumentVisitor
import kotlin.reflect.jvm.internal.impl.descriptors.annotations.AnnotationDescriptor
import kotlin.reflect.jvm.internal.impl.descriptors.findClassAcrossModuleDependencies
import kotlin.reflect.jvm.internal.impl.name.ClassId
import kotlin.reflect.jvm.internal.impl.name.Name
import kotlin.reflect.jvm.internal.impl.resolve.DescriptorUtils
import kotlin.reflect.jvm.internal.impl.types.ErrorUtils
import kotlin.reflect.jvm.internal.impl.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 toString() = value.toString()
}

abstract class IntegerValueConstant 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)
    override fun toString() = value.toString()
}

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)

    override fun equals(other: Any?): Boolean = this === other || value == (other as? ArrayValue)?.value

    override fun hashCode() = value.hashCode()
}

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"

    override fun equals(other: Any?): Boolean = this === other || value == (other as? EnumValue)?.value

    override fun hashCode() = value.hashCode()
}

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)

    override fun equals(other: Any?): Boolean = this === other || value == (other as? IntValue)?.value

    override fun hashCode() = value
}

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)

    override fun toString() = "null"
}

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\""

    override fun equals(other: Any?): Boolean = this === other || value == (other as? StringValue)?.value

    override fun hashCode() = value.hashCode()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy