From e8252ea874e00e56304a434b277e7b6558d344c9 Mon Sep 17 00:00:00 2001 From: Kevin Bierhoff Date: Mon, 25 Nov 2019 12:28:06 -0800 Subject: [PATCH] handle kotlin shifts like bitwise binary ops --- .../expressions/KotlinUBinaryExpression.kt | 5 +- plugins/uast-kotlin/testData/Bitwise.kt | 30 ++++++ plugins/uast-kotlin/testData/Bitwise.log.txt | 98 +++++++++++++++++++ .../uast-kotlin/testData/Bitwise.render.txt | 24 +++++ .../tests/SimpleKotlinRenderLogTest.kt | 2 + 5 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 plugins/uast-kotlin/testData/Bitwise.kt create mode 100644 plugins/uast-kotlin/testData/Bitwise.log.txt create mode 100644 plugins/uast-kotlin/testData/Bitwise.render.txt diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUBinaryExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUBinaryExpression.kt index b2bd9f84038..2cceacb2fb5 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUBinaryExpression.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinUBinaryExpression.kt @@ -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 ) } diff --git a/plugins/uast-kotlin/testData/Bitwise.kt b/plugins/uast-kotlin/testData/Bitwise.kt new file mode 100644 index 00000000000..1b0b94f2d39 --- /dev/null +++ b/plugins/uast-kotlin/testData/Bitwise.kt @@ -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 +} + diff --git a/plugins/uast-kotlin/testData/Bitwise.log.txt b/plugins/uast-kotlin/testData/Bitwise.log.txt new file mode 100644 index 00000000000..81cebbf203c --- /dev/null +++ b/plugins/uast-kotlin/testData/Bitwise.log.txt @@ -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) diff --git a/plugins/uast-kotlin/testData/Bitwise.render.txt b/plugins/uast-kotlin/testData/Bitwise.render.txt new file mode 100644 index 00000000000..782a24b4390 --- /dev/null +++ b/plugins/uast-kotlin/testData/Bitwise.render.txt @@ -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 + } +} diff --git a/plugins/uast-kotlin/tests/SimpleKotlinRenderLogTest.kt b/plugins/uast-kotlin/tests/SimpleKotlinRenderLogTest.kt index 72d6e68b642..ed7443e3cac 100644 --- a/plugins/uast-kotlin/tests/SimpleKotlinRenderLogTest.kt +++ b/plugins/uast-kotlin/tests/SimpleKotlinRenderLogTest.kt @@ -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")