handle kotlin shifts like bitwise binary ops
This commit is contained in:
+4
-1
@@ -35,7 +35,10 @@ class KotlinUBinaryExpression(
|
||||
val BITWISE_OPERATORS = mapOf(
|
||||
"or" to UastBinaryOperator.BITWISE_OR,
|
||||
"and" to UastBinaryOperator.BITWISE_AND,
|
||||
"xor" to UastBinaryOperator.BITWISE_XOR
|
||||
"xor" to UastBinaryOperator.BITWISE_XOR,
|
||||
"shl" to UastBinaryOperator.SHIFT_LEFT,
|
||||
"shr" to UastBinaryOperator.SHIFT_RIGHT,
|
||||
"ushr" to UastBinaryOperator.UNSIGNED_SHIFT_RIGHT
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
fun foo(): Int {
|
||||
val mask: Int = 0x7f
|
||||
val x: Int = 0b1010_1010_1010_1010_1010_1010_1010_1010
|
||||
|
||||
val pos = x and mask
|
||||
val max = x or mask
|
||||
val zebra = x xor mask
|
||||
|
||||
val signed = x shr 2
|
||||
val one = x ushr 31
|
||||
val zero = x shl 32
|
||||
|
||||
return pos + zero - zebra * signed / one
|
||||
}
|
||||
|
||||
fun bar(): Long {
|
||||
val mask: Long = 0x7f
|
||||
val x: Long = 0x5555555555555555
|
||||
|
||||
val pos = x and mask
|
||||
val max = x or mask
|
||||
val zebra = x xor mask
|
||||
|
||||
val signed = x shr 2
|
||||
val one = x ushr 63
|
||||
val zero = x shl 64
|
||||
|
||||
return pos + zero - zebra * signed / one
|
||||
}
|
||||
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
UFile (package = )
|
||||
UClass (name = BitwiseKt)
|
||||
UAnnotationMethod (name = foo)
|
||||
UBlockExpression
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = mask)
|
||||
ULiteralExpression (value = 127)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = x)
|
||||
ULiteralExpression (value = 2863311530)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = pos)
|
||||
UBinaryExpression (operator = &)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
USimpleNameReferenceExpression (identifier = mask)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = max)
|
||||
UBinaryExpression (operator = |)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
USimpleNameReferenceExpression (identifier = mask)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = zebra)
|
||||
UBinaryExpression (operator = ^)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
USimpleNameReferenceExpression (identifier = mask)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = signed)
|
||||
UBinaryExpression (operator = >>)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
ULiteralExpression (value = 2)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = one)
|
||||
UBinaryExpression (operator = >>>)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
ULiteralExpression (value = 31)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = zero)
|
||||
UBinaryExpression (operator = <<)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
ULiteralExpression (value = 32)
|
||||
UReturnExpression
|
||||
UBinaryExpression (operator = -)
|
||||
UBinaryExpression (operator = +)
|
||||
USimpleNameReferenceExpression (identifier = pos)
|
||||
USimpleNameReferenceExpression (identifier = zero)
|
||||
UBinaryExpression (operator = /)
|
||||
UBinaryExpression (operator = *)
|
||||
USimpleNameReferenceExpression (identifier = zebra)
|
||||
USimpleNameReferenceExpression (identifier = signed)
|
||||
USimpleNameReferenceExpression (identifier = one)
|
||||
UAnnotationMethod (name = bar)
|
||||
UBlockExpression
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = mask)
|
||||
ULiteralExpression (value = 127)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = x)
|
||||
ULiteralExpression (value = 6148914691236517205)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = pos)
|
||||
UBinaryExpression (operator = &)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
USimpleNameReferenceExpression (identifier = mask)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = max)
|
||||
UBinaryExpression (operator = |)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
USimpleNameReferenceExpression (identifier = mask)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = zebra)
|
||||
UBinaryExpression (operator = ^)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
USimpleNameReferenceExpression (identifier = mask)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = signed)
|
||||
UBinaryExpression (operator = >>)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
ULiteralExpression (value = 2)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = one)
|
||||
UBinaryExpression (operator = >>>)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
ULiteralExpression (value = 63)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = zero)
|
||||
UBinaryExpression (operator = <<)
|
||||
USimpleNameReferenceExpression (identifier = x)
|
||||
ULiteralExpression (value = 64)
|
||||
UReturnExpression
|
||||
UBinaryExpression (operator = -)
|
||||
UBinaryExpression (operator = +)
|
||||
USimpleNameReferenceExpression (identifier = pos)
|
||||
USimpleNameReferenceExpression (identifier = zero)
|
||||
UBinaryExpression (operator = /)
|
||||
UBinaryExpression (operator = *)
|
||||
USimpleNameReferenceExpression (identifier = zebra)
|
||||
USimpleNameReferenceExpression (identifier = signed)
|
||||
USimpleNameReferenceExpression (identifier = one)
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
public final class BitwiseKt {
|
||||
public static final fun foo() : int {
|
||||
var mask: int = 127
|
||||
var x: int = 2863311530
|
||||
var pos: int = x & mask
|
||||
var max: int = x | mask
|
||||
var zebra: int = x ^ mask
|
||||
var signed: int = x >> 2
|
||||
var one: int = x >>> 31
|
||||
var zero: int = x << 32
|
||||
return pos + zero - zebra * signed / one
|
||||
}
|
||||
public static final fun bar() : long {
|
||||
var mask: long = 127
|
||||
var x: long = 6148914691236517205
|
||||
var pos: long = x & mask
|
||||
var max: long = x | mask
|
||||
var zebra: long = x ^ mask
|
||||
var signed: long = x >> 2
|
||||
var one: long = x >>> 63
|
||||
var zero: long = x << 64
|
||||
return pos + zero - zebra * signed / one
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ class SimpleKotlinRenderLogTest : AbstractKotlinRenderLogTest() {
|
||||
|
||||
@Test fun testDefaultImpls() = doTest("DefaultImpls")
|
||||
|
||||
@Test fun testBitwise() = doTest("Bitwise")
|
||||
|
||||
@Test fun testElvis() = doTest("Elvis")
|
||||
|
||||
@Test fun testPropertyAccessors() = doTest("PropertyAccessors")
|
||||
|
||||
Reference in New Issue
Block a user