JS backend: translate 0L, 1L and -1L to Kotlin.Long constants

This commit is contained in:
Michael Nedzelsky
2014-10-10 16:05:47 +04:00
parent e5c77d1242
commit a63bbf317d
3 changed files with 29 additions and 0 deletions
@@ -43,6 +43,9 @@ public final class Namer {
public static final String LONG_FROM_NUMBER = "fromNumber";
public static final String LONG_TO_NUMBER = "toNumber";
public static final String LONG_FROM_INT = "fromInt";
public static final String LONG_ZERO = "ZERO";
public static final String LONG_ONE = "ONE";
public static final String LONG_NEG_ONE = "NEG_ONE";
public static final String PRIMITIVE_COMPARE_TO = "primitiveCompareTo";
public static final String IS_CHAR = "isChar";
public static final String IS_NUMBER = "isNumber";
@@ -21,19 +21,24 @@ import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.psi.JetConstantExpression;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetUnaryExpression;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.k2js.translate.callTranslator.CallTranslator;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.utils.JsAstUtils;
import org.jetbrains.k2js.translate.utils.TranslationUtils;
import static org.jetbrains.jet.lang.resolve.calls.callUtil.CallUtilPackage.getFunctionResolvedCallWithAssert;
import static org.jetbrains.k2js.translate.general.Translation.translateAsExpression;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getCompileTimeValue;
import static org.jetbrains.k2js.translate.utils.ErrorReportingUtils.message;
import static org.jetbrains.k2js.translate.utils.PsiUtils.getBaseExpression;
import static org.jetbrains.k2js.translate.utils.PsiUtils.getOperationToken;
import static org.jetbrains.k2js.translate.utils.TranslationUtils.*;
@@ -55,6 +60,18 @@ public final class UnaryOperationTranslator {
return type.isNullable() ? sure(translatedExpression, context) : translatedExpression;
}
if (operationToken == JetTokens.MINUS) {
JetExpression baseExpression = getBaseExpression(expression);
if (baseExpression instanceof JetConstantExpression) {
CompileTimeConstant<?> compileTimeValue = context.bindingContext().get(BindingContext.COMPILE_TIME_VALUE, expression);
assert compileTimeValue != null : message(expression, "Expression is not compile time value: " + expression.getText() + " ");
Object value = getCompileTimeValue(context.bindingContext(), expression, compileTimeValue);
if (value instanceof Long) {
return JsAstUtils.newLong((Long) value, context);
}
}
}
if (IncrementTranslator.isIncrement(operationToken)) {
return IncrementTranslator.translate(expression, context);
}
@@ -211,6 +211,15 @@ public final class JsAstUtils {
return new JsNew(Namer.KOTLIN_LONG_NAME_REF, args);
}
else {
if (value == 0) {
return new JsNameRef(Namer.LONG_ZERO, Namer.KOTLIN_LONG_NAME_REF);
}
else if (value == 1) {
return new JsNameRef(Namer.LONG_ONE, Namer.KOTLIN_LONG_NAME_REF);
}
else if (value == -1) {
return new JsNameRef(Namer.LONG_NEG_ONE, Namer.KOTLIN_LONG_NAME_REF);
}
return longFromInt(context.program().getNumberLiteral((int) value));
}
}