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
@@ -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