From d94b47bcd318b6f7db56703565b5857b86128e28 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Fri, 20 Jul 2018 17:28:00 +0300 Subject: [PATCH] Support other kinds of translation for unsigned literals --- .../checkBasicUnsignedLiterals.kt | 11 +++--- .../unsignedLiteralsForMaxLongValue.kt | 10 +++++ .../unsignedLiteralsWithSignedOverflow.kt | 3 +- .../codegen/BlackBoxCodegenTestGenerated.java | 5 +++ .../LightAnalysisModeTestGenerated.java | 5 +++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 +++ .../IrJsCodegenBoxTestGenerated.java | 10 +++++ .../semantics/JsCodegenBoxTestGenerated.java | 10 +++++ .../js/translate/general/Translation.java | 14 +++++-- .../kotlin/js/translate/utils/JsAstUtils.java | 39 +++++++++++++++---- 10 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsForMaxLongValue.kt diff --git a/compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt b/compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt index d1d4fd8dcf7..0a6059b2425 100644 --- a/compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt +++ b/compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt @@ -1,15 +1,16 @@ // WITH_UNSIGNED +// IGNORE_BACKEND: JVM_IR, JS_IR fun box(): String { val good = 42.toUInt() val u1 = 1u val u2 = 2u val u3 = u1 + u2 -// if (u3.toInt() != 3) return "fail" -// -// val max = 0u.dec().toLong() -// val expected = Int.MAX_VALUE * 2L + 1 -// if (max != expected) return "fail" + if (u3.toInt() != 3) return "fail" + + val max = 0u.dec().toLong() + val expected = Int.MAX_VALUE * 2L + 1 + if (max != expected) return "fail" return "OK" } \ No newline at end of file diff --git a/compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsForMaxLongValue.kt b/compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsForMaxLongValue.kt new file mode 100644 index 00000000000..9c2b1771f7f --- /dev/null +++ b/compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsForMaxLongValue.kt @@ -0,0 +1,10 @@ +// WITH_UNSIGNED +// IGNORE_BACKEND: JVM_IR, JS_IR + +fun box(): String { + val maxULong = 0xFFFF_FFFF_FFFF_FFFFuL + val zero = 0uL + if (zero >= maxULong) return "Fail" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt b/compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt index 847c2306849..a954056131b 100644 --- a/compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt +++ b/compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt @@ -1,6 +1,5 @@ -// IGNORE_BACKEND: JVM_IR // WITH_UNSIGNED -// TARGET_BACKEND: JVM +// IGNORE_BACKEND: JVM_IR, JS_IR fun box(): String { val u1: UByte = 255u diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index b750e0ff2a0..2fddfad7584 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -22093,6 +22093,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/unsignedTypes/signedToUnsignedLiteralConversion.kt"); } + @TestMetadata("unsignedLiteralsForMaxLongValue.kt") + public void testUnsignedLiteralsForMaxLongValue() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsForMaxLongValue.kt"); + } + @TestMetadata("unsignedLiteralsWithSignedOverflow.kt") public void testUnsignedLiteralsWithSignedOverflow() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 54168c7a202..ae160947452 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -22093,6 +22093,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/unsignedTypes/signedToUnsignedLiteralConversion.kt"); } + @TestMetadata("unsignedLiteralsForMaxLongValue.kt") + public void testUnsignedLiteralsForMaxLongValue() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsForMaxLongValue.kt"); + } + @TestMetadata("unsignedLiteralsWithSignedOverflow.kt") public void testUnsignedLiteralsWithSignedOverflow() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index df969802f87..bd9e6ef4d40 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -22093,6 +22093,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/unsignedTypes/signedToUnsignedLiteralConversion.kt"); } + @TestMetadata("unsignedLiteralsForMaxLongValue.kt") + public void testUnsignedLiteralsForMaxLongValue() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsForMaxLongValue.kt"); + } + @TestMetadata("unsignedLiteralsWithSignedOverflow.kt") public void testUnsignedLiteralsWithSignedOverflow() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java index c1d4da75239..4acd3c2b316 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java @@ -19948,6 +19948,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/unsignedTypes/signedToUnsignedLiteralConversion.kt"); } + @TestMetadata("unsignedLiteralsForMaxLongValue.kt") + public void testUnsignedLiteralsForMaxLongValue() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsForMaxLongValue.kt"); + } + + @TestMetadata("unsignedLiteralsWithSignedOverflow.kt") + public void testUnsignedLiteralsWithSignedOverflow() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt"); + } + @TestMetadata("unsignedTypePrefixIncrementDecrementBoxing.kt") public void testUnsignedTypePrefixIncrementDecrementBoxing() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypePrefixIncrementDecrementBoxing.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index a1d67ebc050..b230fa4ab23 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -21008,6 +21008,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/unsignedTypes/signedToUnsignedLiteralConversion.kt"); } + @TestMetadata("unsignedLiteralsForMaxLongValue.kt") + public void testUnsignedLiteralsForMaxLongValue() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsForMaxLongValue.kt"); + } + + @TestMetadata("unsignedLiteralsWithSignedOverflow.kt") + public void testUnsignedLiteralsWithSignedOverflow() throws Exception { + runTest("compiler/testData/codegen/box/unsignedTypes/unsignedLiteralsWithSignedOverflow.kt"); + } + @TestMetadata("unsignedTypePrefixIncrementDecrementBoxing.kt") public void testUnsignedTypePrefixIncrementDecrementBoxing() throws Exception { runTest("compiler/testData/codegen/box/unsignedTypes/unsignedTypePrefixIncrementDecrementBoxing.kt"); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java index 18c92ed70ef..702e8611ed5 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java @@ -191,18 +191,24 @@ public final class Translation { return null; } - private static JsExpression translateUnsignedConstant(@NotNull UnsignedValueConstant unsignedConstant, @NotNull TranslationContext context) { + @Nullable + private static JsExpression translateUnsignedConstant( + @NotNull UnsignedValueConstant unsignedConstant, + @NotNull TranslationContext context + ) { if (unsignedConstant instanceof UByteValue) { - return null; + return JsAstUtils.byteToUByte(((UByteValue) unsignedConstant).getValue(), context); } else if (unsignedConstant instanceof UShortValue) { - return null; + return JsAstUtils.shortToUShort(((UShortValue) unsignedConstant).getValue(), context); } else if (unsignedConstant instanceof UIntValue) { return JsAstUtils.intToUInt(((UIntValue) unsignedConstant).getValue(), context); } else if (unsignedConstant instanceof ULongValue) { - return null; + Long value = ((ULongValue) unsignedConstant).getValue(); + JsExpression longExpression = JsAstUtils.newLong(value); + return JsAstUtils.longToULong(longExpression, context); } else { return null; } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsAstUtils.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsAstUtils.java index e42c7da3514..7e4d9358205 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsAstUtils.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsAstUtils.java @@ -28,6 +28,7 @@ import org.jetbrains.kotlin.js.backend.ast.metadata.MetadataProperties; import org.jetbrains.kotlin.js.backend.ast.metadata.SideEffectKind; import org.jetbrains.kotlin.js.translate.context.Namer; import org.jetbrains.kotlin.js.translate.context.TranslationContext; +import org.jetbrains.kotlin.name.ClassId; import org.jetbrains.kotlin.resolve.source.KotlinSourceElementKt; import org.jetbrains.kotlin.util.OperatorNameConventions; @@ -223,15 +224,39 @@ public final class JsAstUtils { } @NotNull - public static JsExpression intToUInt(int value, @NotNull TranslationContext context) { - ClassDescriptor uintClassDescriptor = findClassAcrossModuleDependencies(context.getCurrentModule(), KotlinBuiltIns.FQ_NAMES.uInt); - assert uintClassDescriptor != null; - JsName descName = context.getInnerNameForDescriptor(uintClassDescriptor); - + public static JsExpression byteToUByte(byte value, @NotNull TranslationContext context) { // replace with external builder - JsIntLiteral literal = new JsIntLiteral(value); + return toUnsignedNumber(new JsIntLiteral(value), context, KotlinBuiltIns.FQ_NAMES.uByte); + } - return new JsNew(descName.makeRef(), Collections.singletonList(literal)); + @NotNull + public static JsExpression shortToUShort(short value, @NotNull TranslationContext context) { + // replace with external builder + return toUnsignedNumber(new JsIntLiteral(value), context, KotlinBuiltIns.FQ_NAMES.uShort); + } + + @NotNull + public static JsExpression intToUInt(int value, @NotNull TranslationContext context) { + // replace with external builder + return toUnsignedNumber(new JsIntLiteral(value), context, KotlinBuiltIns.FQ_NAMES.uInt); + } + + @NotNull + public static JsExpression longToULong(@NotNull JsExpression expression, @NotNull TranslationContext context) { + // replace with external builder + return toUnsignedNumber(expression, context, KotlinBuiltIns.FQ_NAMES.uLong); + } + + private static JsExpression toUnsignedNumber( + @NotNull JsExpression expression, + @NotNull TranslationContext context, + @NotNull ClassId unsignedClassId + ) { + ClassDescriptor classDescriptor = findClassAcrossModuleDependencies(context.getCurrentModule(), unsignedClassId); + assert classDescriptor != null : "Class descriptor is null for " + unsignedClassId; + + JsName descName = context.getInnerNameForDescriptor(classDescriptor); + return new JsNew(descName.makeRef(), Collections.singletonList(expression)); } @NotNull