Introduce SIZE_BYTES and SIZE_BITS constants for all integral types

#KT-8247 Fixed
This commit is contained in:
Ilya Gorbunov
2018-06-28 00:44:05 +03:00
parent 31d7aae98d
commit 0c50014e7a
17 changed files with 288 additions and 16 deletions
+10 -9
View File
@@ -19,17 +19,18 @@ package org.jetbrains.kotlin.generators.builtins
import org.jetbrains.kotlin.generators.builtins.ProgressionKind.*
import kotlin.properties.Delegates
enum class PrimitiveType {
BYTE,
CHAR,
SHORT,
INT,
LONG,
FLOAT,
DOUBLE,
BOOLEAN;
enum class PrimitiveType(val byteSize: Int?) {
BYTE(1),
CHAR(2),
SHORT(2),
INT(4),
LONG(8),
FLOAT(null),
DOUBLE(null),
BOOLEAN(null);
val capitalized: String get() = name.toLowerCase().capitalize()
companion object {
val exceptBoolean = PrimitiveType.values().filterNot { it == BOOLEAN }
val onlyNumeric = PrimitiveType.values().filterNot { it == BOOLEAN || it == CHAR }
+20 -7
View File
@@ -59,10 +59,10 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) {
generateDoc(kind)
out.println("public class $className private constructor() : Number(), Comparable<$className> {")
out.print(" companion object ")
out.print(" companion object {")
if (kind == PrimitiveType.FLOAT || kind == PrimitiveType.DOUBLE) {
//val (minValue, maxValue, posInf, negInf, nan) = primitiveConstants(kind)
out.println("""{
out.println("""
/**
* A constant holding the smallest *positive* nonzero value of $className.
*/
@@ -86,12 +86,11 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) {
/**
* A constant holding the "not a number" value of $className.
*/
public val NaN: $className
}""")
public val NaN: $className""")
}
if (kind == PrimitiveType.INT || kind == PrimitiveType.LONG || kind == PrimitiveType.SHORT || kind == PrimitiveType.BYTE) {
val (minValue, maxValue) = primitiveConstants(kind)
out.println("""{
out.println("""
/**
* A constant holding the minimum value an instance of $className can have.
*/
@@ -100,9 +99,23 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) {
/**
* A constant holding the maximum value an instance of $className can have.
*/
public const val MAX_VALUE: $className = $maxValue
}""")
public const val MAX_VALUE: $className = $maxValue""")
}
if (kind.byteSize != null) {
out.println("""
/**
* The number of bytes used to represent an instance of $className in a binary form.
*/
@SinceKotlin("1.3")
public const val SIZE_BYTES: Int = ${kind.byteSize}
/**
* The number of bits used to represent an instance of $className in a binary form.
*/
@SinceKotlin("1.3")
public const val SIZE_BITS: Int = ${kind.byteSize * 8}""")
}
out.println(""" }""")
generateCompareTo(kind)
+10
View File
@@ -56,6 +56,16 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
* A constant holding the maximum value an instance of $className can have.
*/
public const val MAX_VALUE: $className = $className(-1)
/**
* The number of bytes used to represent an instance of $className in a binary form.
*/
public const val SIZE_BYTES: Int = ${type.byteSize}
/**
* The number of bits used to represent an instance of $className in a binary form.
*/
public const val SIZE_BITS: Int = ${type.byteSize * 8}
}""")
generateCompareTo()