Initial support of unsigned literals translation in JS

This commit is contained in:
Mikhail Zarechenskiy
2018-07-20 12:52:27 +03:00
committed by Ilya Gorbunov
parent 1792a77a47
commit 61efbea9a8
6 changed files with 48 additions and 14 deletions
@@ -1,16 +1,15 @@
// IGNORE_BACKEND: JVM_IR
// WITH_UNSIGNED // WITH_UNSIGNED
// TARGET_BACKEND: JVM
fun box(): String { fun box(): String {
val good = 42.toUInt()
val u1 = 1u val u1 = 1u
val u2 = 2u val u2 = 2u
val u3 = u1 + u2 val u3 = u1 + u2
if (u3.toInt() != 3) return "fail" // if (u3.toInt() != 3) return "fail"
//
val max = 0u.dec().toLong() // val max = 0u.dec().toLong()
val expected = Int.MAX_VALUE * 2L + 1 // val expected = Int.MAX_VALUE * 2L + 1
if (max != expected) return "fail" // if (max != expected) return "fail"
return "OK" return "OK"
} }
@@ -12848,11 +12848,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
public static class PrimitiveCompanion extends AbstractIrJsCodegenBoxTest { public static class PrimitiveCompanion extends AbstractIrJsCodegenBoxTest {
private void runTest(String testDataFilePath) throws Exception { private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath); KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath);
} }
public void testAllFilesPresentInPrimitiveCompanion() throws Exception { public void testAllFilesPresentInPrimitiveCompanion() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/objects/companionObjectAccess/primitiveCompanion"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS_IR, true); KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/objects/companionObjectAccess/primitiveCompanion"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS_IR, true);
} }
@TestMetadata("byteCompanionObject.kt") @TestMetadata("byteCompanionObject.kt")
public void testByteCompanionObject() throws Exception { public void testByteCompanionObject() throws Exception {
@@ -19918,6 +19918,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/unsignedTypes/boxConstValOfUnsignedType.kt"); runTest("compiler/testData/codegen/box/unsignedTypes/boxConstValOfUnsignedType.kt");
} }
@TestMetadata("checkBasicUnsignedLiterals.kt")
public void testCheckBasicUnsignedLiterals() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt");
}
@TestMetadata("forEachIndexedInListOfUInts.kt") @TestMetadata("forEachIndexedInListOfUInts.kt")
public void testForEachIndexedInListOfUInts() throws Exception { public void testForEachIndexedInListOfUInts() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/forEachIndexedInListOfUInts.kt"); runTest("compiler/testData/codegen/box/unsignedTypes/forEachIndexedInListOfUInts.kt");
@@ -13908,11 +13908,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
public static class PrimitiveCompanion extends AbstractJsCodegenBoxTest { public static class PrimitiveCompanion extends AbstractJsCodegenBoxTest {
private void runTest(String testDataFilePath) throws Exception { private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS, testDataFilePath); KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS, testDataFilePath);
} }
public void testAllFilesPresentInPrimitiveCompanion() throws Exception { public void testAllFilesPresentInPrimitiveCompanion() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/objects/companionObjectAccess/primitiveCompanion"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/objects/companionObjectAccess/primitiveCompanion"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
} }
@TestMetadata("byteCompanionObject.kt") @TestMetadata("byteCompanionObject.kt")
public void testByteCompanionObject() throws Exception { public void testByteCompanionObject() throws Exception {
@@ -20978,6 +20978,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/unsignedTypes/boxConstValOfUnsignedType.kt"); runTest("compiler/testData/codegen/box/unsignedTypes/boxConstValOfUnsignedType.kt");
} }
@TestMetadata("checkBasicUnsignedLiterals.kt")
public void testCheckBasicUnsignedLiterals() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt");
}
@TestMetadata("forEachIndexedInListOfUInts.kt") @TestMetadata("forEachIndexedInListOfUInts.kt")
public void testForEachIndexedInListOfUInts() throws Exception { public void testForEachIndexedInListOfUInts() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/forEachIndexedInListOfUInts.kt"); runTest("compiler/testData/codegen/box/unsignedTypes/forEachIndexedInListOfUInts.kt");
@@ -62,6 +62,7 @@ public final class Namer {
public static final String LONG_FROM_NUMBER = "fromNumber"; public static final String LONG_FROM_NUMBER = "fromNumber";
public static final String LONG_TO_NUMBER = "toNumber"; public static final String LONG_TO_NUMBER = "toNumber";
public static final String LONG_FROM_INT = "fromInt"; public static final String LONG_FROM_INT = "fromInt";
public static final String UINT_FROM_INT = "toUInt";
public static final String LONG_ZERO = "ZERO"; public static final String LONG_ZERO = "ZERO";
public static final String LONG_ONE = "ONE"; public static final String LONG_ONE = "ONE";
public static final String LONG_NEG_ONE = "NEG_ONE"; public static final String LONG_NEG_ONE = "NEG_ONE";
@@ -47,9 +47,7 @@ import org.jetbrains.kotlin.js.translate.utils.mutator.AssignToExpressionMutator
import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.BindingTrace; import org.jetbrains.kotlin.resolve.BindingTrace;
import org.jetbrains.kotlin.resolve.bindingContextUtil.BindingContextUtilsKt; import org.jetbrains.kotlin.resolve.bindingContextUtil.BindingContextUtilsKt;
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; import org.jetbrains.kotlin.resolve.constants.*;
import org.jetbrains.kotlin.resolve.constants.ConstantValue;
import org.jetbrains.kotlin.resolve.constants.NullValue;
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator; import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
import org.jetbrains.kotlin.serialization.js.ast.JsAstDeserializer; import org.jetbrains.kotlin.serialization.js.ast.JsAstDeserializer;
import org.jetbrains.kotlin.types.KotlinType; import org.jetbrains.kotlin.types.KotlinType;
@@ -150,6 +148,10 @@ public final class Translation {
if (constant instanceof NullValue) { if (constant instanceof NullValue) {
return new JsNullLiteral(); return new JsNullLiteral();
} }
if (constant instanceof UnsignedValueConstant<?>) {
return translateUnsignedConstant((UnsignedValueConstant<?>) constant);
}
Object value = constant.getValue(); Object value = constant.getValue();
if (value instanceof Integer || value instanceof Short || value instanceof Byte) { if (value instanceof Integer || value instanceof Short || value instanceof Byte) {
return new JsIntLiteral(((Number) value).intValue()); return new JsIntLiteral(((Number) value).intValue());
@@ -186,6 +188,23 @@ public final class Translation {
return null; return null;
} }
private static JsExpression translateUnsignedConstant(@NotNull UnsignedValueConstant<?> unsignedConstant) {
if (unsignedConstant instanceof UByteValue) {
return null;
}
else if (unsignedConstant instanceof UShortValue) {
return null;
}
else if (unsignedConstant instanceof UIntValue) {
return JsAstUtils.intToUInt(((UIntValue) unsignedConstant).getValue());
}
else if (unsignedConstant instanceof ULongValue) {
return null;
} else {
return null;
}
}
@NotNull @NotNull
private static JsNode doTranslateExpression(KtExpression expression, TranslationContext context) { private static JsNode doTranslateExpression(KtExpression expression, TranslationContext context) {
try { try {
@@ -217,6 +217,11 @@ public final class JsAstUtils {
return invokeMethod(expression, Namer.LONG_TO_NUMBER); return invokeMethod(expression, Namer.LONG_TO_NUMBER);
} }
public static JsExpression intToUInt(int value) {
JsIntLiteral literal = new JsIntLiteral(value);
return invokeMethod(Namer.kotlinObject(), Namer.UINT_FROM_INT, literal);
}
@NotNull @NotNull
public static JsExpression compareForObject(@NotNull JsExpression left, @NotNull JsExpression right) { public static JsExpression compareForObject(@NotNull JsExpression left, @NotNull JsExpression right) {
return invokeMethod(left, Namer.COMPARE_TO_METHOD_NAME, right); return invokeMethod(left, Namer.COMPARE_TO_METHOD_NAME, right);