Support other kinds of translation for unsigned literals

This commit is contained in:
Mikhail Zarechenskiy
2018-07-20 17:28:00 +03:00
committed by Ilya Gorbunov
parent d1df5f5783
commit d94b47bcd3
10 changed files with 94 additions and 18 deletions
@@ -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"
}
@@ -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"
}
@@ -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
@@ -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");
@@ -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");
@@ -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");
@@ -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");
@@ -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");
@@ -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;
}
@@ -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