use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class KotlinComponentUnwrapper method doUnwrap.
@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
KtElement targetElement = (KtElement) element;
KtExpression expressionToUnwrap = getExpressionToUnwrap(targetElement);
assert expressionToUnwrap != null;
KtElement enclosingElement = getEnclosingElement(targetElement);
context.extractFromExpression(expressionToUnwrap, enclosingElement);
context.delete(enclosingElement);
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class PatternTranslator method translateIsExpression.
@NotNull
public JsExpression translateIsExpression(@NotNull KtIsExpression expression) {
KtExpression left = expression.getLeftHandSide();
JsExpression expressionToCheck = Translation.translateAsExpression(left, context());
KotlinType leftType = context().bindingContext().getType(left);
if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
expressionToCheck = JsAstUtils.charToBoxedChar(expressionToCheck);
}
KtTypeReference typeReference = expression.getTypeReference();
assert typeReference != null;
JsExpression result = translateIsCheck(expressionToCheck, typeReference);
if (result == null)
return JsLiteral.getBoolean(!expression.isNegated());
if (expression.isNegated()) {
return not(result);
}
return result;
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class OverloadedAssignmentTranslator method overloadedMethodInvocation.
@NotNull
private JsExpression overloadedMethodInvocation(AccessTranslator accessTranslator) {
JsBlock innerBlock = new JsBlock();
TranslationContext innerContext = context().innerBlock(innerBlock);
JsExpression oldValue = accessTranslator.translateAsGet();
JsBlock argumentBlock = new JsBlock();
TranslationContext argumentContext = innerContext.innerBlock(argumentBlock);
KtExpression argumentPsi = expression.getRight();
assert argumentPsi != null;
JsExpression argument = Translation.translateAsExpression(argumentPsi, argumentContext);
if (!argumentBlock.isEmpty()) {
oldValue = innerContext.defineTemporary(oldValue);
innerContext.addStatementsToCurrentBlockFrom(argumentBlock);
}
Map<KtExpression, JsExpression> aliases = new HashMap<KtExpression, JsExpression>();
aliases.put(argumentPsi, argument);
innerContext = innerContext.innerContextWithAliasesForExpressions(aliases);
JsExpression result = CallTranslator.translate(innerContext, resolvedCall, oldValue);
context().addStatementsToCurrentBlockFrom(innerBlock);
return result;
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class UnaryOperationTranslator method translate.
@NotNull
public static JsExpression translate(@NotNull KtUnaryExpression expression, @NotNull TranslationContext context) {
IElementType operationToken = expression.getOperationReference().getReferencedNameElementType();
if (operationToken == KtTokens.EXCLEXCL) {
KtExpression baseExpression = getBaseExpression(expression);
JsExpression translatedExpression = translateAsExpression(baseExpression, context);
return sure(translatedExpression, context);
}
if (operationToken == KtTokens.MINUS) {
KtExpression baseExpression = getBaseExpression(expression);
if (baseExpression instanceof KtConstantExpression) {
CompileTimeConstant<?> compileTimeValue = ConstantExpressionEvaluator.getConstant(expression, context.bindingContext());
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);
}
JsExpression baseExpression = TranslationUtils.translateBaseExpression(context, expression);
if (isExclForBinaryEqualLikeExpr(expression, baseExpression)) {
return translateExclForBinaryEqualLikeExpr((JsBinaryOperation) baseExpression);
}
ResolvedCall<? extends FunctionDescriptor> resolvedCall = CallUtilKt.getFunctionResolvedCallWithAssert(expression, context.bindingContext());
return CallTranslator.translate(context, resolvedCall, baseExpression);
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class ArrayAccessTranslator method getCached.
@NotNull
@Override
public AccessTranslator getCached() {
Map<KtExpression, JsExpression> aliases = new HashMap<KtExpression, JsExpression>();
JsExpression arrayExpression = context().cacheExpressionIfNeeded(getArrayExpression());
aliases.put(expression.getArrayExpression(), arrayExpression);
for (KtExpression ktExpression : expression.getIndexExpressions()) {
JsExpression jsExpression = context().cacheExpressionIfNeeded(Translation.translateAsExpression(ktExpression, context()));
aliases.put(ktExpression, jsExpression);
}
return new CachedArrayAccessTranslator(expression, context().innerContextWithAliasesForExpressions(aliases), arrayExpression);
}
Aggregations