From 4ac7be9fa5e376a6ba9c539ca2c08b8eb764fe3e Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 28 Jan 2017 09:22:27 +0300 Subject: [PATCH] Retract bitwise operators from builtins for Byte and Short and add them as extensions in kotlin.experimental package in stdlib. #KT-16030 --- .../evaluate/OperationsMapGenerated.kt | 8 --- core/builtins/native/kotlin/Primitives.kt | 30 +---------- .../kotlin/generators/builtins/primitives.kt | 2 +- .../kotlin/experimental/bitwiseOperations.kt | 54 +++++++++++++++++++ 4 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 libraries/stdlib/src/kotlin/experimental/bitwiseOperations.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/OperationsMapGenerated.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/OperationsMapGenerated.kt index f56738ccbba..ba4d73891a5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/OperationsMapGenerated.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/OperationsMapGenerated.kt @@ -28,7 +28,6 @@ internal val unaryOperations: HashMap, Pair, Pair, Function1>>( unaryOperation(BOOLEAN, "not", { a -> a.not() }, emptyUnaryFun), unaryOperation(BOOLEAN, "toString", { a -> a.toString() }, emptyUnaryFun), - unaryOperation(BYTE, "inv", { a -> a.inv() }, emptyUnaryFun), unaryOperation(BYTE, "toByte", { a -> a.toByte() }, emptyUnaryFun), unaryOperation(BYTE, "toChar", { a -> a.toChar() }, emptyUnaryFun), unaryOperation(BYTE, "toDouble", { a -> a.toDouble() }, emptyUnaryFun), @@ -89,7 +88,6 @@ internal val unaryOperations: HashMap, Pair a.toString() }, emptyUnaryFun), unaryOperation(LONG, "unaryMinus", { a -> a.unaryMinus() }, { a -> a.unaryMinus() }), unaryOperation(LONG, "unaryPlus", { a -> a.unaryPlus() }, emptyUnaryFun), - unaryOperation(SHORT, "inv", { a -> a.inv() }, emptyUnaryFun), unaryOperation(SHORT, "toByte", { a -> a.toByte() }, emptyUnaryFun), unaryOperation(SHORT, "toChar", { a -> a.toChar() }, emptyUnaryFun), unaryOperation(SHORT, "toDouble", { a -> a.toDouble() }, emptyUnaryFun), @@ -111,7 +109,6 @@ internal val binaryOperations: HashMap, Pair a.equals(b) }, emptyBinaryFun), binaryOperation(BOOLEAN, BOOLEAN, "or", { a, b -> a.or(b) }, emptyBinaryFun), binaryOperation(BOOLEAN, BOOLEAN, "xor", { a, b -> a.xor(b) }, emptyBinaryFun), - binaryOperation(BYTE, BYTE, "and", { a, b -> a.and(b) }, { a, b -> a.and(b) }), binaryOperation(BYTE, BYTE, "compareTo", { a, b -> a.compareTo(b) }, emptyBinaryFun), binaryOperation(BYTE, DOUBLE, "compareTo", { a, b -> a.compareTo(b) }, emptyBinaryFun), binaryOperation(BYTE, FLOAT, "compareTo", { a, b -> a.compareTo(b) }, emptyBinaryFun), @@ -137,7 +134,6 @@ internal val binaryOperations: HashMap, Pair a.mod(b) }, { a, b -> a.mod(b) }), binaryOperation(BYTE, LONG, "mod", { a, b -> a.mod(b) }, { a, b -> a.mod(b) }), binaryOperation(BYTE, SHORT, "mod", { a, b -> a.mod(b) }, { a, b -> a.mod(b) }), - binaryOperation(BYTE, BYTE, "or", { a, b -> a.or(b) }, { a, b -> a.or(b) }), binaryOperation(BYTE, BYTE, "plus", { a, b -> a.plus(b) }, { a, b -> a.add(b) }), binaryOperation(BYTE, DOUBLE, "plus", { a, b -> a.plus(b) }, emptyBinaryFun), binaryOperation(BYTE, FLOAT, "plus", { a, b -> a.plus(b) }, emptyBinaryFun), @@ -156,7 +152,6 @@ internal val binaryOperations: HashMap, Pair a.times(b) }, { a, b -> a.multiply(b) }), binaryOperation(BYTE, LONG, "times", { a, b -> a.times(b) }, { a, b -> a.multiply(b) }), binaryOperation(BYTE, SHORT, "times", { a, b -> a.times(b) }, { a, b -> a.multiply(b) }), - binaryOperation(BYTE, BYTE, "xor", { a, b -> a.xor(b) }, { a, b -> a.xor(b) }), binaryOperation(CHAR, CHAR, "compareTo", { a, b -> a.compareTo(b) }, emptyBinaryFun), binaryOperation(CHAR, ANY, "equals", { a, b -> a.equals(b) }, emptyBinaryFun), binaryOperation(CHAR, CHAR, "minus", { a, b -> a.minus(b) }, emptyBinaryFun), @@ -346,7 +341,6 @@ internal val binaryOperations: HashMap, Pair a.times(b) }, { a, b -> a.multiply(b) }), binaryOperation(LONG, INT, "ushr", { a, b -> a.ushr(b) }, emptyBinaryFun), binaryOperation(LONG, LONG, "xor", { a, b -> a.xor(b) }, { a, b -> a.xor(b) }), - binaryOperation(SHORT, SHORT, "and", { a, b -> a.and(b) }, { a, b -> a.and(b) }), binaryOperation(SHORT, BYTE, "compareTo", { a, b -> a.compareTo(b) }, emptyBinaryFun), binaryOperation(SHORT, DOUBLE, "compareTo", { a, b -> a.compareTo(b) }, emptyBinaryFun), binaryOperation(SHORT, FLOAT, "compareTo", { a, b -> a.compareTo(b) }, emptyBinaryFun), @@ -372,7 +366,6 @@ internal val binaryOperations: HashMap, Pair a.mod(b) }, { a, b -> a.mod(b) }), binaryOperation(SHORT, LONG, "mod", { a, b -> a.mod(b) }, { a, b -> a.mod(b) }), binaryOperation(SHORT, SHORT, "mod", { a, b -> a.mod(b) }, { a, b -> a.mod(b) }), - binaryOperation(SHORT, SHORT, "or", { a, b -> a.or(b) }, { a, b -> a.or(b) }), binaryOperation(SHORT, BYTE, "plus", { a, b -> a.plus(b) }, { a, b -> a.add(b) }), binaryOperation(SHORT, DOUBLE, "plus", { a, b -> a.plus(b) }, emptyBinaryFun), binaryOperation(SHORT, FLOAT, "plus", { a, b -> a.plus(b) }, emptyBinaryFun), @@ -391,7 +384,6 @@ internal val binaryOperations: HashMap, Pair a.times(b) }, { a, b -> a.multiply(b) }), binaryOperation(SHORT, LONG, "times", { a, b -> a.times(b) }, { a, b -> a.multiply(b) }), binaryOperation(SHORT, SHORT, "times", { a, b -> a.times(b) }, { a, b -> a.multiply(b) }), - binaryOperation(SHORT, SHORT, "xor", { a, b -> a.xor(b) }, { a, b -> a.xor(b) }), binaryOperation(STRING, STRING, "compareTo", { a, b -> a.compareTo(b) }, emptyBinaryFun), binaryOperation(STRING, ANY, "equals", { a, b -> a.equals(b) }, emptyBinaryFun), binaryOperation(STRING, INT, "get", { a, b -> a.get(b) }, emptyBinaryFun), diff --git a/core/builtins/native/kotlin/Primitives.kt b/core/builtins/native/kotlin/Primitives.kt index 8b9f0f13c1e..007a506f321 100644 --- a/core/builtins/native/kotlin/Primitives.kt +++ b/core/builtins/native/kotlin/Primitives.kt @@ -185,19 +185,6 @@ public class Byte private constructor() : Number(), Comparable { /** Creates a range from this value to the specified [other] value. */ public operator fun rangeTo(other: Long): LongRange - /** Performs a bitwise AND operation between the two values. */ - @SinceKotlin("1.1") - public infix fun and(other: Byte): Byte - /** Performs a bitwise OR operation between the two values. */ - @SinceKotlin("1.1") - public infix fun or(other: Byte): Byte - /** Performs a bitwise XOR operation between the two values. */ - @SinceKotlin("1.1") - public infix fun xor(other: Byte): Byte - /** Inverts the bits in this value/ */ - @SinceKotlin("1.1") - public fun inv(): Byte - public override fun toByte(): Byte public override fun toChar(): Char public override fun toShort(): Short @@ -374,19 +361,6 @@ public class Short private constructor() : Number(), Comparable { /** Creates a range from this value to the specified [other] value. */ public operator fun rangeTo(other: Long): LongRange - /** Performs a bitwise AND operation between the two values. */ - @SinceKotlin("1.1") - public infix fun and(other: Short): Short - /** Performs a bitwise OR operation between the two values. */ - @SinceKotlin("1.1") - public infix fun or(other: Short): Short - /** Performs a bitwise XOR operation between the two values. */ - @SinceKotlin("1.1") - public infix fun xor(other: Short): Short - /** Inverts the bits in this value/ */ - @SinceKotlin("1.1") - public fun inv(): Short - public override fun toByte(): Byte public override fun toChar(): Char public override fun toShort(): Short @@ -575,7 +549,7 @@ public class Int private constructor() : Number(), Comparable { public infix fun or(other: Int): Int /** Performs a bitwise XOR operation between the two values. */ public infix fun xor(other: Int): Int - /** Inverts the bits in this value/ */ + /** Inverts the bits in this value. */ public fun inv(): Int public override fun toByte(): Byte @@ -766,7 +740,7 @@ public class Long private constructor() : Number(), Comparable { public infix fun or(other: Long): Long /** Performs a bitwise XOR operation between the two values. */ public infix fun xor(other: Long): Long - /** Inverts the bits in this value/ */ + /** Inverts the bits in this value. */ public fun inv(): Long public override fun toByte(): Byte diff --git a/generators/src/org/jetbrains/kotlin/generators/builtins/primitives.kt b/generators/src/org/jetbrains/kotlin/generators/builtins/primitives.kt index 42e22e415e7..aa487d752c6 100644 --- a/generators/src/org/jetbrains/kotlin/generators/builtins/primitives.kt +++ b/generators/src/org/jetbrains/kotlin/generators/builtins/primitives.kt @@ -123,7 +123,7 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) { if (kind == PrimitiveType.INT || kind == PrimitiveType.LONG) { generateBitShiftOperators(className) } - if (kind == PrimitiveType.INT || kind == PrimitiveType.LONG || kind == PrimitiveType.BYTE || kind == PrimitiveType.SHORT) { + if (kind == PrimitiveType.INT || kind == PrimitiveType.LONG /* || kind == PrimitiveType.BYTE || kind == PrimitiveType.SHORT */) { generateBitwiseOperators(className, since = if (kind == PrimitiveType.BYTE || kind == PrimitiveType.SHORT) "1.1" else null) } diff --git a/libraries/stdlib/src/kotlin/experimental/bitwiseOperations.kt b/libraries/stdlib/src/kotlin/experimental/bitwiseOperations.kt new file mode 100644 index 00000000000..178256a60ef --- /dev/null +++ b/libraries/stdlib/src/kotlin/experimental/bitwiseOperations.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2010-2017 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.experimental + +/** Performs a bitwise AND operation between the two values. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun Byte.and(other: Byte): Byte = (this.toInt() and other.toInt()).toByte() +/** Performs a bitwise OR operation between the two values. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun Byte.or(other: Byte): Byte = (this.toInt() or other.toInt()).toByte() +/** Performs a bitwise XOR operation between the two values. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun Byte.xor(other: Byte): Byte = (this.toInt() xor other.toInt()).toByte() +/** Inverts the bits in this value. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun Byte.inv(): Byte = (this.toInt().inv()).toByte() + + +/** Performs a bitwise AND operation between the two values. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun Short.and(other: Short): Short = (this.toInt() and other.toInt()).toShort() +/** Performs a bitwise OR operation between the two values. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun Short.or(other: Short): Short = (this.toInt() or other.toInt()).toShort() +/** Performs a bitwise XOR operation between the two values. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun Short.xor(other: Short): Short = (this.toInt() xor other.toInt()).toShort() +/** Inverts the bits in this value. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun Short.inv(): Short = (this.toInt().inv()).toShort() + +