use of org.jetbrains.kotlin.js.translate.context.TemporaryVariable in project kotlin by JetBrains.
the class Translation method translateAsExpression.
@NotNull
public static JsExpression translateAsExpression(@NotNull KtExpression expression, @NotNull TranslationContext context, @NotNull JsBlock block) {
JsNode jsNode = translateExpression(expression, context, block);
if (jsNode instanceof JsExpression) {
KotlinType expressionType = context.bindingContext().getType(expression);
return unboxIfNeeded((JsExpression) jsNode, expressionType != null && KotlinBuiltIns.isCharOrNullableChar(expressionType));
}
assert jsNode instanceof JsStatement : "Unexpected node of type: " + jsNode.getClass().toString();
if (BindingContextUtilsKt.isUsedAsExpression(expression, context.bindingContext())) {
TemporaryVariable result = context.declareTemporary(null);
AssignToExpressionMutator saveResultToTemporaryMutator = new AssignToExpressionMutator(result.reference());
block.getStatements().add(mutateLastExpression(jsNode, saveResultToTemporaryMutator));
return result.reference();
}
block.getStatements().add(convertToStatement(jsNode));
return JsLiteral.NULL;
}
use of org.jetbrains.kotlin.js.translate.context.TemporaryVariable in project kotlin by JetBrains.
the class PatternTranslator method translateCastExpression.
@NotNull
public JsExpression translateCastExpression(@NotNull KtBinaryExpressionWithTypeRHS expression) {
assert isCastExpression(expression) : "Expected cast expression, got " + expression;
KtExpression left = expression.getLeft();
JsExpression expressionToCast = Translation.translateAsExpression(left, context());
KtTypeReference typeReference = expression.getRight();
assert typeReference != null : "Cast expression must have type reference";
KotlinType leftType = context().bindingContext().getType(left);
if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
expressionToCast = JsAstUtils.charToBoxedChar(expressionToCast);
}
TemporaryVariable temporary = context().declareTemporary(expressionToCast);
JsExpression isCheck = translateIsCheck(temporary.assignmentExpression(), typeReference);
if (isCheck == null)
return expressionToCast;
JsExpression onFail;
if (isSafeCast(expression)) {
onFail = JsLiteral.NULL;
} else {
JsExpression throwCCEFunRef = Namer.throwClassCastExceptionFunRef();
onFail = new JsInvocation(throwCCEFunRef);
}
JsExpression result = new JsConditional(isCheck, temporary.reference(), onFail);
KotlinType expressionType = context().bindingContext().getType(expression);
if (expressionType != null && KotlinBuiltIns.isCharOrNullableChar(expressionType)) {
result = JsAstUtils.boxedCharToChar(result);
}
return result;
}
use of org.jetbrains.kotlin.js.translate.context.TemporaryVariable in project kotlin by JetBrains.
the class IncrementTranslator method asPostfix.
//TODO: decide if this expression can be optimised in case of direct access (not property)
@NotNull
private JsExpression asPostfix() {
// code fragment: expr(a++)
// generate: expr( (t1 = a, t2 = t1, a = t1.inc(), t2) )
TemporaryVariable t1 = context().declareTemporary(accessTranslator.translateAsGet());
accessBlock.getStatements().add(t1.assignmentStatement());
JsExpression variableReassignment = variableReassignment(context().innerBlock(accessBlock), t1.reference());
accessBlock.getStatements().add(JsAstUtils.asSyntheticStatement(variableReassignment));
JsExpression result;
if (accessBlock.getStatements().size() == 2) {
result = AstUtil.newSequence(t1.assignmentExpression(), variableReassignment, t1.reference());
} else {
context().getCurrentBlock().getStatements().addAll(accessBlock.getStatements());
result = t1.reference();
}
MetadataProperties.setSynthetic(result, true);
return result;
}
Aggregations