[FIR] Support unsigned integer literals

This commit is contained in:
simon.ogorodnik
2020-03-24 02:22:53 +03:00
parent 0afbf25943
commit 27136ef8be
12 changed files with 111 additions and 91 deletions
@@ -9,14 +9,23 @@ sealed class FirConstKind<T>(val asString: kotlin.String) {
object Null : FirConstKind<Nothing?>("Null")
object Boolean : FirConstKind<kotlin.Boolean>("Boolean")
object Char : FirConstKind<kotlin.Char>("Char")
object Byte : FirConstKind<kotlin.Byte>("Byte")
object UnsignedByte : FirConstKind<kotlin.Byte>("UByte")
object Short : FirConstKind<kotlin.Short>("Short")
object UnsignedShort : FirConstKind<kotlin.Short>("UShort")
object Int : FirConstKind<kotlin.Int>("Int")
object UnsignedInt : FirConstKind<kotlin.Int>("UInt")
object Long : FirConstKind<kotlin.Long>("Long")
object UnsignedLong : FirConstKind<kotlin.Long>("ULong")
object String : FirConstKind<kotlin.String>("String")
object Float : FirConstKind<kotlin.Float>("Float")
object Double : FirConstKind<kotlin.Double>("Double")
object IntegerLiteral : FirConstKind<kotlin.Long>("IntegerLiteral")
object UnsignedIntegerLiteral : FirConstKind<kotlin.Long>("UnsignedIntegerLiteral")
override fun toString() = asString
}
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.types.model.SimpleTypeMarker
class ConeIntegerLiteralTypeImpl : ConeIntegerLiteralType {
override val possibleTypes: Collection<ConeClassLikeType>
constructor(value: Long, nullability: ConeNullability = ConeNullability.NOT_NULL) : super(value, nullability) {
constructor(value: Long, isUnsigned: Boolean, nullability: ConeNullability = ConeNullability.NOT_NULL) : super(value, isUnsigned, nullability) {
possibleTypes = mutableListOf()
fun checkBoundsAndAddPossibleType(classId: ClassId, range: LongRange) {
@@ -30,15 +30,26 @@ class ConeIntegerLiteralTypeImpl : ConeIntegerLiteralType {
checkBoundsAndAddPossibleType(StandardClassIds.Short, SHORT_RANGE)
}
addSignedPossibleTypes()
// TODO: add support of unsigned types
fun addUnsignedPossibleType() {
checkBoundsAndAddPossibleType(StandardClassIds.UInt, UINT_RANGE)
possibleTypes += createType(StandardClassIds.ULong)
checkBoundsAndAddPossibleType(StandardClassIds.UByte, UBYTE_RANGE)
checkBoundsAndAddPossibleType(StandardClassIds.UShort, USHORT_RANGE)
}
if (isUnsigned) {
addUnsignedPossibleType()
} else {
addSignedPossibleTypes()
}
}
private constructor(
value: Long,
possibleTypes: Collection<ConeClassLikeType>,
isUnsigned: Boolean,
nullability: ConeNullability = ConeNullability.NOT_NULL
) : super(value, nullability) {
) : super(value, isUnsigned, nullability) {
this.possibleTypes = possibleTypes
}
@@ -68,6 +79,10 @@ class ConeIntegerLiteralTypeImpl : ConeIntegerLiteralType {
private val BYTE_RANGE = Byte.MIN_VALUE.toLong()..Byte.MAX_VALUE.toLong()
private val SHORT_RANGE = Short.MIN_VALUE.toLong()..Short.MAX_VALUE.toLong()
private val UBYTE_RANGE = UByte.MIN_VALUE.toLong()..UByte.MAX_VALUE.toLong()
private val USHORT_RANGE = UShort.MIN_VALUE.toLong()..UShort.MAX_VALUE.toLong()
private val UINT_RANGE = UInt.MIN_VALUE.toLong()..UInt.MAX_VALUE.toLong()
private val COMPARABLE_TAG = ConeClassLikeLookupTagImpl(StandardClassIds.Comparable)
fun findCommonSuperType(types: Collection<SimpleTypeMarker>): SimpleTypeMarker? {
@@ -114,7 +129,7 @@ class ConeIntegerLiteralTypeImpl : ConeIntegerLiteralType {
Mode.COMMON_SUPER_TYPE -> left.possibleTypes intersect right.possibleTypes
Mode.INTERSECTION_TYPE -> left.possibleTypes union right.possibleTypes
}
return ConeIntegerLiteralTypeImpl(left.value, possibleTypes)
return ConeIntegerLiteralTypeImpl(left.value, possibleTypes, left.isUnsigned)
}
private fun fold(left: ConeIntegerLiteralType, right: SimpleTypeMarker): SimpleTypeMarker? =
@@ -59,5 +59,10 @@ fun ConeClassLikeType.toConstKind(): FirConstKind<*>? = when (lookupTag.classId)
StandardClassIds.Short -> FirConstKind.Short
StandardClassIds.Int -> FirConstKind.Int
StandardClassIds.Long -> FirConstKind.Long
StandardClassIds.UInt -> FirConstKind.UnsignedInt
StandardClassIds.ULong -> FirConstKind.UnsignedLong
StandardClassIds.UShort -> FirConstKind.UnsignedShort
StandardClassIds.UByte -> FirConstKind.UnsignedByte
else -> null
}